forked from niceue/tpl.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpl.debug.js
58 lines (54 loc) · 2.03 KB
/
tpl.debug.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
/*! tpl.js 0.2.0, github.com/niceue/tpl.js */
/* 类似于PHP的嵌入方式, 可以嵌入js语句
模板语法:
嵌入传入的变量: <%=xxx%> , 注意,xxx变量不能为javascript关键字
嵌入任意js语句:<% if(xxx){ %> foo <%}else{%> bar <%}%>
调用:
var html = tpl('#tpl_id', data);
console.log(html);
*/
(function (root, factory) {
//AMD and CMD (RequireJS, OzJS, curl, SeaJS ...)
typeof define === 'function' && define(factory);
//Node.js and Browser global
(typeof exports !== 'undefined' ? exports : root).tpl = factory();
}(this, function () {
function Compiler(html) {
html = html || '';
if (/^#\w+$/.test(html)) html = document.getElementById(html.substring(1)).innerHTML;
var begin = '<%',
end = '%>',
trim = function(str) {
return str.trim ? str.trim() : str.replace(/^\s*|\s*$/g, '');
},
ecp = function(str){
return str.replace(/('|\\|\r?\n)/g, '\\$1');
},
str = "var __='',echo=function(s){__+=s};with(_$||{}){",
blen = begin.length,
elen = end.length,
b = html.indexOf(begin),
e,
tmp;
while(b != -1) {
e = html.indexOf(end);
if(e < b) break; //出错后不再编译
str += "__+='" + ecp(html.substring(0, b)) + "';";
tmp = trim(html.substring(b+blen, e));
if( tmp.indexOf('=') === 0 ) { //模板变量
tmp = tmp.substring(1);
str += "typeof(" + tmp + ")!='undefined'&&(__+=" + tmp + ");";
} else { //js代码
str += tmp;
}
html = html.substring(e + elen);
b = html.indexOf(begin);
}
str += "__+='" + ecp(html) + "'}return __";
this.render = new Function("_$", str);
}
return function(html, data) {
var me = new Compiler(html);
return data ? me.render(data) : me;
};
}));