1
1
/**
2
- @overview
3
- @author Rafał Wrzeszcz <[email protected] >
4
- @license Apache License 2.0 - See file 'LICENSE.md' in this project.
2
+ * @file Wrapper for underscore's template utility to allow loading templates from files.
3
+ * @author Rafał Wrzeszcz <[email protected] >
4
+ * @author <a href="mailto:[email protected] ">Matthew Christopher Kastor-Inare III</a>
5
+ * @license Apache License 2.0 - See file 'LICENSE.md' in this project.
6
+ * @change 2012-10-20 cache and settings are no longer private fields.
7
+ * This was done so that plugins could use this class without messing up
8
+ * the final output when template name collisions occur. i.e. each instance
9
+ * has it's own template cache. The settings were made specific to each
10
+ * instance so that users of this class could redefine the underscore
11
+ * template settings if they want to.
5
12
*/
6
13
7
14
var _ = require ( 'underscore' ) ,
8
15
fs = require ( 'fs' ) ,
9
16
path = require ( 'path' ) ;
10
17
11
- // override default settings
12
- var settings = {
13
- evaluate : / < \? j s ( [ \s \S ] + ?) \? > / g,
14
- interpolate : / < \? j s = ( [ \s \S ] + ?) \? > / g,
15
- escape : / < \? j s ~ ( [ \s \S ] + ?) \? > / g
16
- } ;
17
18
18
19
/**
19
20
@module jsdoc/template
@@ -27,6 +28,14 @@ var settings = {
27
28
exports . Template = function ( path ) {
28
29
this . path = path ;
29
30
this . layout = null ;
31
+ this . cache = { } ;
32
+ // override default template tag settings
33
+ this . settings = {
34
+ evaluate : / < \? j s ( [ \s \S ] + ?) \? > / g,
35
+ interpolate : / < \? j s = ( [ \s \S ] + ?) \? > / g,
36
+ escape : / < \? j s ~ ( [ \s \S ] + ?) \? > / g
37
+ } ;
38
+
30
39
} ;
31
40
32
41
/** Loads template from given file.
@@ -35,11 +44,9 @@ exports.Template = function(path) {
35
44
*/
36
45
exports . Template . prototype . load = function ( file ) {
37
46
var _path = path . join ( this . path , file ) ;
38
- return _ . template ( fs . readFileSync ( _path ) , null , settings ) ;
47
+ return _ . template ( fs . readFileSync ( _path ) , null , this . settings ) ;
39
48
} ;
40
49
41
- // templates cache
42
- var cache = { } ;
43
50
44
51
/**
45
52
Renders template using given data.
@@ -52,12 +59,12 @@ var cache = {};
52
59
*/
53
60
exports . Template . prototype . partial = function ( file , data ) {
54
61
// load template into cache
55
- if ( ! ( file in cache ) ) {
56
- cache [ file ] = this . load ( file ) ;
62
+ if ( ! ( file in this . cache ) ) {
63
+ this . cache [ file ] = this . load ( file ) ;
57
64
}
58
65
59
66
// keep template helper context
60
- return cache [ file ] . call ( this , data ) ;
67
+ return this . cache [ file ] . call ( this , data ) ;
61
68
} ;
62
69
63
70
/**
0 commit comments