8行代码的模板字符串替换函数

1. 特点

  • 无依赖
  • 无检查
  • 无错误处理
  • 无逻辑
  • 无配置

2. 代码

1
2
3
4
5
6
7
8
function render(tpl, data){
var re = /{{([^}]+)?}}/;
var match = '';
while(match = re.exec(tpl)){
tpl = tpl.replace(match[0],data[match[1]]);
}
return tpl;
}

3. demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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