- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemplate.js
More file actions
Latest commit
115 lines (99 loc) · 3.7 KB
/
Copy pathtemplate.js
File metadata and controls
115 lines (99 loc) · 3.7 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* node-template
* http://github.com/graphnode/node-template/
* by Diogo Gomes - MIT Licensed
*
* Based off of:
* - Chad Etzel - http://github.com/jazzychad/template.node.js/
* - John Resig - http://ejohn.org/blog/javascript-micro-templating/
*/
/*jslint node: true, browser: true, evil: true, regexp: true, nomen: true */
(function(module){
'use strict';
varfs,exports,templateFn;
if(require){
fs=require('fs');
}
exports={
useCache: true,
cache: {}
};
templateFn=function(str,data,callback){
// Move argument around, data should be an object not a function
if(typeof(data)==="function"){
callback=data;
data=undefined;
}
// Figure out if we're getting a template, or if we need to
// load the template - and be sure to cache the result.
varfn,functionArgs,functionBody,result;
if(fs&&str!==''&&!/[\t\r\n%]/.test(str)){
if(!callback){
fn=templateFn(fs.readFileSync(str,'utf8'),data,callback);
}else{
fs.readFile(str,{encoding: 'utf8'},function(err,contents){
if(err){
throwerr;
}
templateFn(contents,data,callback);
});
return;
}
}else{
if(((data&&data.cache)||exports.useCache)&&exports.cache[str]){
fn=exports.cache[str];
}else{
functionArgs="obj";
functionBody="var p=[],print=function(){p.push.apply(p,arguments);};"+
"obj=obj||{};"+
// Introduce the data as local variables using with(){}
"with(obj){p.push('"+
// Convert the template into pure JavaScript
str.split("'").join("\\'")
.replace(/\r\n|\n/mg,'\\n')
.replace(/<%([\s\S]*?)%>/mg,function(m,t){return'<%'+t.split("\\'").join("'").split("\\n").join("\n")+'%>';})
.replace(/<%=(.+?)%>/g,"',$1,'")
.split("<%").join("');")
.split("%>").join("p.push('")+
"');}return p.join('');";
// Generate a reusable function that will serve as a template
// generator (and which will be cached).
try{
fn=newFunction(functionArgs,functionBody);
}catch(e1){
if(callback){
callback(e1,null);
return;
}else{
throwe1;
}
}
if((data&&data.cache)||exports.useCache){
exports.cache[str]=fn;
}
}
}
// Provide some "basic" currying to the user
if(callback){
result=null;
try{
result=data ? fn(data) : fn;
}catch(e2){
callback(e2,null);
return;
}
callback(null,result);
}else{
returndata ? fn(data) : fn;
}
};
exports.render=exports.__express=templateFn;
// Backwards compatibility and semantics.
exports.create=templateFn;
// Export template engine or inject it into browser window.
if(module){
module.exports=exports;
}elseif(window){
window.template=exports;
}
}(module));