我试图将一个对象数组作为参数传递给一个部分:
{{> partial [{title: "hello", year: "2015"}, {title: "hello2" year: "2015"}] }}
然后在部分:
<div>
{{#each this}}
<label>{{title}}</label>
<label>{{year}}</label>
{{/each}}
</div>
...但什么也没有出现。
有没有办法将数组数据传递给部分?
提前致谢。
最佳答案
创建一个解析 JSON 的助手并用这个上下文包装你的部分。
模板:
{{#getJsonContext '[{"title": "hello", "year": "2015"}, {"title": "hello2" "year": "2015"}]'}}
{{> partial this }}
{{/getJsonContext}}
请注意,引用名称以及 JSON 字符串中的值。
helper :
Handlebars.registerHelper('getJsonContext', function(data, options) {
return options.fn(JSON.parse(data));
});
信用:https://github.com/assemble/assemble/issues/228#issuecomment-20853985
关于javascript - 将对象数组传递给部分-handlebars.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30372693/