handlebars是个前端模版组件。特别是在js渲染dom的时候比较好用
比如我有个数据:
{
"list": [{
"id": 1,
"name": "test1"
}, {
"id": 2,
"name": "test2"
}],
"type": "score"
}
在handlebars直接写each就好
{{#each list}}
user {{name}} id is {{id}}
list type is {{type}}
{{/each}}
但是handlebars不会递归查找,也就是说在each作用域里面只能取到list元素的属性,直接写外部属性type是不行的。
要改成这样:
{{#each list}}
user {{name}} id is {{id}}
list type is {{../type}}
{{/each}}
另外,handlebars不支持业务逻辑,连a==1这种都不支持,如果要做这样的逻辑判断,就要自定义函数
Handlebars.registerHelper('ifEq', function(arg1, arg2, options) {
return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
});
用法:
{{#ifEq type 'score'}}
{{/ifEq}}