forked from kof/jqtpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjqtpl.express.js
72 lines (61 loc) · 1.69 KB
/
jqtpl.express.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var jqtpl = require('./jqtpl');
// add express specific tags
jqtpl.tag.partial = {
_default: { $2: 'null' },
open: 'if($notnull_1){_=_.concat($item.data.partial($1,$2));}'
};
jqtpl.tag.layout = {
_default: { $2: 'null' },
open: 'if($notnull_1){_=_.concat($item.data.layout($1,$2));}'
};
/**
* Print debugging information to stdout, export print method,
* to let other commonjs systems without "util" to mock it.
* @param {*} data any kind of data.
* @export
*/
exports.debug = function(data) {
var util = require('util');
util.debug.apply(util, arguments);
};
/**
* Support Express compile method (used by Express >= 2.0)
*
* @param {string} markup html string.
* @param {Object} options
* `filename` Used by `cache` to key caches.
* `scope` Function execution context.
* `debug` Output generated function body.
*
* @return {string} rendered html string.
* @export
*/
exports.compile = function(markup, options) {
options = options || {};
var name = options.filename || markup;
// express calls compile if the template have to be recompiled
// so we have to clean cache before compile
delete jqtpl.template[name];
// precompile the template and cache it using filename
jqtpl.template(name, markup);
if (options.debug) {
// print the template generator fn
exports.debug(jqtpl.template[name]);
}
return function render(locals) {
return jqtpl.tmpl(name, locals, options);
};
};
/**
* Clear cache
* @export
*/
exports.clearCache = function() {
var cache = jqtpl.template,
name;
for (name in cache) {
if (cache.hasOwnProperty(name)) {
delete cache[name];
}
}
};