Skip to content

Commit f826b6f

Browse files
committed
Optimized code
1 parent 4e7cf09 commit f826b6f

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed

index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@ var fs = require("fs");
33
function compiler(html) {
44
html = html || '';
55
if (/\.(?=tpl|html)$/.test(html)) html = fs.readFileSync(html);
6-
var begin = '<#',
7-
end = '#>',
8-
ecp = function(str){
6+
7+
var ecp = function(str){
98
return str.replace(/('|\\)/g, '\\$1').replace(/\r\n/g, '\\r\\n').replace(/\n/g, '\\n');
109
},
11-
str = "var __='',echo=function(s){__+=s},include=function(t,d){__+=tpl(t,d||_$)};with($||{}){",
12-
blen = begin.length,
13-
elen = end.length,
14-
b = html.indexOf(begin),
15-
e,
10+
begin = '<#',
11+
end = '#>',
12+
blen = begin.length, elen = end.length,
13+
b = html.indexOf(begin), e,
14+
str = "var __='',___,echo=function(s){__+=s},include=function(t,d){__+=tpl(t,d||_$)};with($||{}){",
1615
tmp;
16+
1717
while(b != -1) {
1818
e = html.indexOf(end);
1919
if(e < b) break; //出错后不再编译
2020
str += "__+='" + ecp(html.substring(0, b)) + "';";
2121
tmp = html.substring(b+blen, e).trim();
2222
if( tmp.indexOf('=') === 0 ) { //模板变量
2323
tmp = tmp.substring(1);
24-
str += "typeof (" + tmp + ")!='undefined'&&(__+=" + tmp + ");";
24+
str += "___=" + tmp + ";typeof ___!=='undefined'&&(__+=___);";
2525
} else { //js代码
2626
str += tmp + ";";
2727
}

tpl.debug.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
/*! tpl.js 0.3.0, github.com/niceue/tpl.js */
1+
/*! tpl.js 0.3.1, github.com/niceue/tpl.js */
22

33
/* 类似于PHP的嵌入方式, 可以嵌入js语句
44
模板语法:
55
嵌入传入的变量: <#=xxx#> , 注意,xxx变量不能为javascript关键字
66
嵌入任意js语句:<#if(xxx){#> foo <#}else{#> bar <#}#>
7+
内置 echo() 和 include() 两个方法
8+
<###>之间的内容会被跳过编译<###>
79
调用:
810
方式一(直接输出结果):tpl(template, data)
911
方式二(预编译,二次调用传入data):tpl(template)(data)
@@ -21,29 +23,28 @@
2123

2224
function compiler(html) {
2325
html = html || '';
24-
if (/^#\w+$/.test(html)) html = document.getElementById(html.substring(1)).innerHTML;
25-
var begin = tpl.begin,
26-
end = tpl.end,
27-
v = tpl.variable,
28-
arg1 = v || "$",
29-
trim = function(str) {
26+
if (html.charAt(0)==='#') html = document.getElementById(html.substring(1)).innerHTML;
27+
var trim = function(str) {
3028
return str.trim ? str.trim() : str.replace(/^\s*|\s*$/g, '');
3129
},
3230
ecp = function(str){
3331
return str.replace(/('|\\|\r?\n)/g, '\\$1');
3432
},
35-
str = "var "+ arg1 +"="+ arg1 +"||this,__='',echo=function(s){__+=s},include=function(t,d){__+=tpl(t).call(d||"+ arg1 +")};"+ (v?"":"with($||{}){"),
36-
blen = begin.length,
37-
elen = end.length,
38-
b = html.indexOf(begin),
39-
e,
33+
begin = tpl.begin,
34+
end = tpl.end,
35+
v = tpl.variable,
36+
arg1 = v || "$",
37+
str = "var "+ arg1 +"="+ arg1 +"||this,__='',___,\
38+
echo=function(s){__+=s},\
39+
include=function(t,d){__+=tpl(t).call(d||"+ arg1 +")};"+ (v?"":"with($||{}){"),
40+
blen = begin.length, elen = end.length,
41+
b = html.indexOf(begin), e,
4042
skip,
4143
tmp;
4244

4345
while(b != -1) {
4446
e = skip ? b + blen : html.indexOf(end);
4547
if(e < b) break; //出错后不再编译
46-
4748
str += "__+='" + ecp(html.substring(0, b)) + "';";
4849

4950
if (skip) {
@@ -53,25 +54,23 @@
5354
tmp = trim(html.substring(b+blen, e));
5455
if ('#'===tmp) {
5556
skip = 1;
56-
}
57-
else if( tmp.indexOf('=') === 0 ) { //模板变量
57+
} else if( tmp.indexOf('=') === 0 ) { //模板变量
5858
tmp = tmp.substring(1);
59-
str += "typeof (" + tmp + ")!=='undefined'&&(__+=" + tmp + ");";
59+
str += "___=" + tmp + ";typeof ___!=='undefined'&&(__+=___);";
6060
} else { //js代码
61-
str += tmp + ";";
61+
str += "\n" + tmp + "\n";
6262
}
6363
}
6464

6565
html = html.substring(e + elen);
6666
b = html.indexOf( begin + (skip ? '#'+end : '') );
6767
}
6868
str += "__+='" + ecp(html) + "'"+ (v?";":"}") +"return __";
69-
console.log(str)
7069
return new Function(arg1, str);
7170
}
7271

7372
//Browser global
7473
window.tpl = tpl;
7574
//AMD and CMD (RequireJS, OzJS, curl, SeaJS ...)
7675
typeof define === 'function' && define('tpl',[],function(){return tpl});
77-
})(window);
76+
})(this);

tpl.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)