-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtppl.js
42 lines (40 loc) · 1.2 KB
/
tppl.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
/**
* tppl.js 极致性能的 JS 模板引擎
* Github:https://github.com/jojoin/tppl
* 作者:杨捷
* 邮箱:[email protected]
*
* @param tpl {String} 模板字符串
* @param data {Object} 模板数据(不传或为null时返回渲染方法)
*
* @return {String} 渲染结果
* @return {Function} 渲染方法
*
*/
exports.tppl = function(tpl, data){
var fn = function(d) {
var i, k = [], v = [];
for (i in d) {
k.push(i);
v.push(d[i]);
};
return (new Function(k, fn.$)).apply(d, v);
};
if(!fn.$){
var tpls = tpl.split('[:');
fn.$ = "var $=''";
for(var t = 0;t < tpls.length;t++){
var p = tpls[t].split(':]');
if(t!=0){
fn.$ += '='==p[0].charAt(0)
? "+("+p[0].substr(1)+")"
: ";"+p[0].replace(/\r\n/g, '')+"$=$"
}
// 支持 <pre> 和 [::] 包裹的 js 代码
fn.$ += "+'"+p[p.length-1].replace(/\'/g,"\\'").replace(/\r\n/g, '\\n').replace(/\n/g, '\\n').replace(/\r/g, '\\n')+"'";
}
fn.$ += ";return $;";
// log(fn.$);
}
return data ? fn(data) : fn;
}