特点
- 无依赖
- 无检查
- 无错误处理
- 无逻辑
- 无配置
代码
function render(tpl, data){
var re = /{{([^}]+)?}}/;
var match = ‘‘;
while(match = re.exec(tpl)){
tpl = tpl.replace(match[0],data[match[1]]);
}
return tpl;
}
demo
var tpl = ‘/cube_xinbao_dial_result/{{action}}/{{report_type}}/{{query}}/?userId={{userId}}‘;
var data = {report_type:1, query: ‘2323‘, action: ‘todolist‘,userId: ‘23234234‘}
function render(tpl, data){
var re = /{{([^}]+)?}}/;
var match = ‘‘;
while(match = re.exec(tpl)){
tpl = tpl.replace(match[0],data[match[1]]);
}
return tpl;
}
console.log(render(tpl,data));
> /cube_xinbao_dial_result/todolist/1/2323/?userId=23234234
原文地址:https://www.cnblogs.com/jlfw/p/12491887.html
时间: 2024-10-21 17:15:56