一、自带的常用方法
1.遍历 each
var data = [{id:'first',name:'vuturn'},{id:'second',name:'John'}];
{{#each this}}
<span>{{this.id}}</span>
<span>{{this.name}}</span>
{{/each}}
2. 是否相等 equal
{{#equal key 1}}
<span>Test</span>
{{/equal}}
3.if
{{#if key}}
<span>Test</span>
{{/if}}
4.调用父级变量
../key
二、注册helper
1.注册遍历数组显示索引方法
注册:
Handlebars.registerHelper('addKey',function(index){
return index + 1;
});
使用:
{{#each this}}
{{addKey @index}}
{{/each}}
2.注册if (v1 == v2){}else{}方法
注册:
Handlebars.registerHelper('ifCond',function(v1 , v2 ,options){
if(v1 == v2){
return options.fn(this);
}
return options.inverse(this);
});
使用:
{{#ifCond key value}}
<span>test</span>
{{else}}
<span>test</span>
{{/ifCond}}
参考: http://handlebarsjs.com/