-
Notifications
You must be signed in to change notification settings - Fork 149
/
footprint.js
executable file
·33 lines (31 loc) · 1.22 KB
/
footprint.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
var footprint = exports;
// Default template settings, uses ASP/PHP/JSP delimiters, change the
// following template settings to use alternative delimiters.
var templateSettings = {
evaluate : /<%([\s\S]+?)%>/g,
interpolate : /<%=([\s\S]+?)%>/g
};
footprint.compile = function(str, settings) {
var c = settings || templateSettings;
var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
'with(obj||{}){__p.push(\'' +
str.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
.replace(c.interpolate, function(match, code) {
return "'," + code.replace(/\\'/g, "'") + ",'";
})
.replace(c.evaluate || null, function(match, code) {
return "');" + code.replace(/\\'/g, "'")
.replace(/[\r\n\t]/g, ' ') + "__p.push('";
})
.replace(/\r/g, '\\r')
.replace(/\n/g, '\\n')
.replace(/\t/g, '\\t')
+ "');}return __p.join('');";
return new Function('obj', tmpl);
};
// Preserves template method for compatible with legacy call.
footprint.template = function (str, data) {
var compilied = footprint.compile(str);
return compilied(data);
};