使用简单的
代码{{#each array}}
{{@index}}: {{this}}
{{/each}}
出现大量错误。对象{{@key}}
也是如此。为什么会这样?
答案 0 :(得分:5)
查看源代码(https://github.com/meteor/meteor/blob/master/packages/handlebars/parse.js):与Meteor一起打包的Handlebars版本支持{{@ ..}}
表达式看起来不像。
答案 1 :(得分:5)
这对我来说绝对是一种挫败感。与此同时,我制作了一个把手助手来解析任何名为'key'和'value'的对象:
Handlebars.registerHelper('key_value', function(context, options) {
var result = [];
_.each(context, function(value, key, list){
result.push({key:key, value:value});
})
return result;
});
这将与#each
运算符一起使用,如:
<dl class="attributes">
{{#each key_value attributes}}
<dt>{{key}}</dt><dd>{{value}}</dd>
{{/each}}
</dl>
(我也将其发布到相关的Using @index in meteor #each iterator doesn't work)