-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtpl.debug.js
76 lines (68 loc) · 2.72 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*! tpl.js 0.3.1, github.com/niceue/tpl.js */
/* 类似于PHP的嵌入方式, 可以嵌入js语句
模板语法:
嵌入传入的变量: <#=xxx#> , 注意,xxx变量不能为javascript关键字
嵌入任意js语句:<#if(xxx){#> foo <#}else{#> bar <#}#>
内置 echo() 和 include() 两个方法
<###>之间的内容会被跳过编译<###>
调用:
方式一(直接输出结果):tpl(template, data)
方式二(预编译,二次调用传入data):tpl(template)(data)
其中,如果template变量传入#id,会自动获取innerHTML
*/
(function (window) {
function tpl(html, data) {
var fn = compiler(html);
return data ? fn(data) : fn;
}
tpl.begin = '<#';
tpl.end = '#>';
function compiler(html) {
html = html || '';
if (html.charAt(0)==='#') html = document.getElementById(html.substring(1)).innerHTML;
var trim = function(str) {
return str.trim ? str.trim() : str.replace(/^\s*|\s*$/g, '');
},
ecp = function(str){
return str.replace(/('|\\|\r?\n)/g, '\\$1');
},
begin = tpl.begin,
end = tpl.end,
v = tpl.variable,
arg1 = v || "$",
str = "var "+ arg1 +"="+ arg1 +"||this,__='',___,\
echo=function(s){__+=s},\
include=function(t,d){__+=tpl(t).call(d||"+ arg1 +")};"+ (v?"":"with($||{}){"),
blen = begin.length, elen = end.length,
b = html.indexOf(begin), e,
skip,
tmp;
while(b != -1) {
e = skip ? b + blen : html.indexOf(end);
if(e < b) break; //出错后不再编译
str += "__+='" + ecp(html.substring(0, b)) + "';";
if (skip) {
html = html.substring(blen+elen+1);
skip--;
} else {
tmp = trim(html.substring(b+blen, e));
if ('#'===tmp) {
skip = 1;
} else if( tmp.indexOf('=') === 0 ) { //模板变量
tmp = tmp.substring(1);
str += "___=" + tmp + ";typeof ___!=='undefined'&&(__+=___);";
} else { //js代码
str += "\n" + tmp + "\n";
}
}
html = html.substring(e + elen);
b = html.indexOf( begin + (skip ? '#'+end : '') );
}
str += "__+='" + ecp(html) + "'"+ (v?";":"}") +"return __";
return new Function(arg1, str);
}
//Browser global
window.tpl = tpl;
//AMD and CMD (RequireJS, OzJS, curl, SeaJS ...)
typeof define === 'function' && define('tpl',[],function(){return tpl});
})(this);