使用handlebarsjs管理部分内容

时间:2014-01-30 17:10:52

标签: javascript handlebars.js

我想要使用带数组的partials,还要使用带数组的主模板。

主要模板:

<div id="notificationsRows" class="wrapper">
            {{#each notifications}}
                {{>notification}}
            {{/each}}
        </div>

行通知:

<div class="box-container-info">
    div class="thumbnail" style="background-image: url('{{image}}');"></div>
    <div class="info">{{message}}</div>
</div>

如果我生成主,我可以生成:

var context = {
    notifications: [{...},{...}]
}

这里没有问题,但如果我想添加更多通知,我就不能使用像

这样的数组调用部分
var moreContextForPartial =  [{...},{...}]; // This not works

我只能像对象一样添加每个对象,而不是数组:

var moreContextForPartial =  {...}; // This works

有些想法?

1 个答案:

答案 0 :(得分:0)

你要求部分 - 具有单行的上下文 - 来获取行数,但它不能。 尝试询问具有迭代器的主模板来执行该任务:

var moreNotifications =  [{...},{...}];
var wrapper = {notifications: moreNotifications };

var source   = $("#notificationRows").html();
var template = Handlebars.compile(source);

var html = template(wrapper);