codekart
Version:
Web application framework for Node.js
37 lines (33 loc) • 1 kB
JavaScript
//github: https://github.com/yangjiePro/tppl
C.tppl = function(tpl, data, fast){
var fn = function (d, f) {
if(f||fast){
fn.$$ = fn.$$ || new Function(fn.$);
return fn.$$.apply(d);
}else{
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.$){
fn.$ = 'var $="";';
var tpls = tpl.replace(/[\r\t\n]/g, " ").split('[:')
, i = 0
while(i<tpls.length){
var p = tpls[i];
if(i){
var x = p.indexOf(':]');
fn.$ += p.substr(0, x);
p = p.substr(x+2)
}
fn.$ += "$+='"+p.replace(/\'/g,"\\'").replace(/\[\=\:(.*?)\:\]/g, "'+$1+'")+"';";
i++;
}
fn.$ += "return $";
}
return data ? fn(data) : fn;
};