将一组对象传递给partial - handlebars.js [英] Passing an array of objects to a partial - handlebars.js
本文介绍了将一组对象传递给partial - handlebars.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
{{>}我试图将一个对象数组作为参数传递给partial。部分[{title:hello,year:2015},{title:hello2year:2015}]}}
然后在部分:
< div>
{{#each this}}
< label> {{title}}< / label>
< label> {{year}}< / label>
{{/ each}}
< / div>
...但没有显示。
<有没有办法将数组数据传递给部分?
在此先感谢。
解决方案
创建一个帮助程序,解析JSON并用此上下文包装您的部分。 p>
Template:
{{#getJsonContext'[{title: hello,year:2015},{title:hello2year:2015}]'}}
{{>部分本}}
{{/ getJsonContext}}
请注意名称被引用为以及JSON字符串中的值。
助手:
Handlebars.registerHelper('getJsonContext',函数(数据,选项){
return options.fn(JSON.parse(data));
});
贷: https://github.com/assemble/assemble/issues/228#issuecomment-20853985
Im trying to pass an array of objects into a partial as an argument:
{{> partial [{title: "hello", year: "2015"}, {title: "hello2" year: "2015"}] }}
and then on the partial:
<div>
{{#each this}}
<label>{{title}}</label>
<label>{{year}}</label>
{{/each}}
</div>
... but nothing shows up.
Is there a way to pass array data to a partial? Thanks in advance.
解决方案
Create a helper that parses the JSON and wrap your partial with this context.
Template:
{{#getJsonContext '[{"title": "hello", "year": "2015"}, {"title": "hello2" "year": "2015"}]'}}
{{> partial this }}
{{/getJsonContext}}
Note that the names are quoted as well as the values in the JSON string.
Helper:
Handlebars.registerHelper('getJsonContext', function(data, options) {
return options.fn(JSON.parse(data));
});
Credit: https://github.com/assemble/assemble/issues/228#issuecomment-20853985
这篇关于将一组对象传递给partial - handlebars.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文