Skip to content

Commit

Permalink
Optimized code
Browse files Browse the repository at this point in the history
  • Loading branch information
niceue committed Sep 28, 2014
1 parent 4e7cf09 commit f826b6f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ var fs = require("fs");
function compiler(html) {
html = html || '';
if (/\.(?=tpl|html)$/.test(html)) html = fs.readFileSync(html);
var begin = '<#',
end = '#>',
ecp = function(str){

var ecp = function(str){
return str.replace(/('|\\)/g, '\\$1').replace(/\r\n/g, '\\r\\n').replace(/\n/g, '\\n');
},
str = "var __='',echo=function(s){__+=s},include=function(t,d){__+=tpl(t,d||_$)};with($||{}){",
blen = begin.length,
elen = end.length,
b = html.indexOf(begin),
e,
begin = '<#',
end = '#>',
blen = begin.length, elen = end.length,
b = html.indexOf(begin), e,
str = "var __='',___,echo=function(s){__+=s},include=function(t,d){__+=tpl(t,d||_$)};with($||{}){",
tmp;

while(b != -1) {
e = html.indexOf(end);
if(e < b) break; //出错后不再编译
str += "__+='" + ecp(html.substring(0, b)) + "';";
tmp = html.substring(b+blen, e).trim();
if( tmp.indexOf('=') === 0 ) { //模板变量
tmp = tmp.substring(1);
str += "typeof (" + tmp + ")!='undefined'&&(__+=" + tmp + ");";
str += "___=" + tmp + ";typeof ___!=='undefined'&&(__+=___);";
} else { //js代码
str += tmp + ";";
}
Expand Down
37 changes: 18 additions & 19 deletions tpl.debug.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/*! tpl.js 0.3.0, github.com/niceue/tpl.js */
/*! 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)
Expand All @@ -21,29 +23,28 @@

function compiler(html) {
html = html || '';
if (/^#\w+$/.test(html)) html = document.getElementById(html.substring(1)).innerHTML;
var begin = tpl.begin,
end = tpl.end,
v = tpl.variable,
arg1 = v || "$",
trim = function(str) {
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');
},
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,
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) {
Expand All @@ -53,25 +54,23 @@
tmp = trim(html.substring(b+blen, e));
if ('#'===tmp) {
skip = 1;
}
else if( tmp.indexOf('=') === 0 ) { //模板变量
} else if( tmp.indexOf('=') === 0 ) { //模板变量
tmp = tmp.substring(1);
str += "typeof (" + tmp + ")!=='undefined'&&(__+=" + tmp + ");";
str += "___=" + tmp + ";typeof ___!=='undefined'&&(__+=___);";
} else { //js代码
str += tmp + ";";
str += "\n" + tmp + "\n";
}
}

html = html.substring(e + elen);
b = html.indexOf( begin + (skip ? '#'+end : '') );
}
str += "__+='" + ecp(html) + "'"+ (v?";":"}") +"return __";
console.log(str)
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});
})(window);
})(this);
4 changes: 2 additions & 2 deletions tpl.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f826b6f

Please sign in to comment.