您的位置:首页 > Web前端 > JavaScript

JavaScript 页面模板引擎

2015-10-26 15:37 453 查看
varTemplateEngine=function(html,options){
varre=/<%([^%>]+)?%>/g,reExp=/(^()?(if|for|else|switch|case|break|{|}))(.*)?/g,code='varr=[];\n',cursor=0;
varadd=function(line,js){
js?(code+=line.match(reExp)?line+'\n':'r.push('+line+');\n'):
(code+=line!=''?'r.push("'+line.replace(/"/g,'\\"')+'");\n':'');
returnadd;
}
while(match=re.exec(html)){
add(html.slice(cursor,match.index))(match[1],true);
cursor=match.index+match[0].length;
}
add(html.substr(cursor,html.length-cursor));
code+='returnr.join("");';
returnnewFunction(code.replace(/[\r\t\n]/g,'')).apply(options);
}</%([^%>


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: