将一组对象传递给部分--handlebars.js

时间:2015-05-21 11:40:56

标签: javascript handlebars.js templating

我试图将一个对象数组作为参数传递给partial:

{{> partial [{title: "hello", year: "2015"}, {title: "hello2" year: "2015"}] }}

然后是部分:

<div>

  {{#each this}}
    <label>{{title}}</label>
    <label>{{year}}</label>
  {{/each}}

</div>

......但没有出现任何事情。

有没有办法将数组数据传递给部分? 提前谢谢。

2 个答案:

答案 0 :(得分:2)

创建一个帮助程序,解析JSON并使用此上下文包装部分。

模板:

Handlebars.registerHelper('getJsonContext', function(data, options) {
   return options.fn(JSON.parse(data));
});

请注意,引用的名称以及JSON字符串中的值。

助手:

 //Generate Outlook MailItem Object, "item"
    //If item.DownloadState is not 1,
        //item.Display
        //item.Close(1)
        //Perform end of code operations
        //Call Function that is identical to Application_NewMailEx but is not Application_NewMailEx because Application_NewMailEx is a function that is thrown during the incoming mail event.
    //Else,
        //Perform Intended Code

信用:https://github.com/assemble/assemble/issues/228#issuecomment-20853985

答案 1 :(得分:1)

这应该有效

{{> partial items=this.something }}

Handlebars.registerPartial(
    'partial', 
    "<div>{{#each items}}<label>{{title}}</label><label>{{year}}</label>{{/each}}</div>"
);

输入:

{
    something: [{title: "hello", year: "2015"}, {title: "hello2", year: "2015"}]
}

此外,JSON对象中存在问题。

相关问题