Eup*_*phe 2 javascript handlebars.js meteor spacebars
用简单的代码
{{#each array}}
{{@index}}: {{this}}
{{/each}}
Run Code Online (Sandbox Code Playgroud)
出现大量错误.情况与此相同{{@key}}的对象.为什么会这样?
查看源代码(https://github.com/meteor/meteor/blob/master/packages/handlebars/parse.js):Meteor{{@ ..}}打包的Handlebars版本看起来不支持表达式集.
这对我来说绝对是一种挫败感.与此同时,我制作了一个把手助手来解析任何名为'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;
});
Run Code Online (Sandbox Code Playgroud)
这将与#each运营商一起使用,如:
<dl class="attributes">
{{#each key_value attributes}}
<dt>{{key}}</dt><dd>{{value}}</dd>
{{/each}}
</dl>
Run Code Online (Sandbox Code Playgroud)
(我也刚刚将这个发布到相关的使用@index in meteor #each iterator不起作用)