[英]Query on Block Helpers in Handlebars.js
I have a product block in a template like this : 我在这样的模板中有一个积木:
<script type="text/x-handlebars-template" id="tmpl-person">
<div class="product">
<!-- Product details here -->
</div>
</script>
What I want to do is if from the array of person I get as data , after every three persons , I want to insert a container called <div class="row-fluid"></div>
and three persons inside it.. then a row-fluid container and three persons inside it. 我想做的是,如果从我作为数据获得的人的数组中,每三个人之后,我想在其中插入一个名为
<div class="row-fluid"></div>
的容器。然后是一个排液容器,里面有三个人。 How can I achieve this using helpers ? 如何使用助手实现此目的? Thanks for help.
感谢帮助。
You could use something like this 你可以用这样的东西
Handlebars.registerHelper('each', function(context, block) {
var ret = "";
for(var i=0, j=context.length; i<j; i++) {
ret = ret + "<li>" + block(context[i]) + "</li>";
}
if( i % 3 == 0)
ret = ret + <div class="row-fluid"></div>
return ret;
});
And you could define your custom iterator like following 您可以定义自定义迭代器,如下所示
<script type="text/x-handlebars-template" id="tmpl-person">
{{#each productInfo}}
<div class="product">
<!-- Product details here -->
</div>
{{/each}}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.