当流星使用{{#each}}模板逻辑仅解析每个X集合条目时,是否可以插入特定数据?
例:
HTML
{{#each collection}}
<div>
<p>{{collectionparam1}} - {{collectionparam2}}</p>
{{#if **collection[only multiple of 2]**}}
<p>I show this {{collectionparam3}} only each 2 entries</p>
{{/if}}
</div>
{{/each}}
结果:
Billy - 24
Joe - 12
I show this banana only each 2 entries
Bob - 88
Maria - 5
I show this cherry only each 2 entries
Samantha - 102
Henry - 43
I show this apple only each 2 entries
如果可能的话,我需要在{{#if}}逻辑上做什么?
感谢您的帮助。
答案 0 :(得分:2)
这类似于this question。试一试:
<强> HTML 强>
{{#each collection}}
<div>
<p>{{collectionparam1}} - {{collectionparam2}}</p>
{{#if shouldShow @index}}
<p>I show this {{collectionparam3}} only each 2 entries</p>
{{/if}}
</div>
{{/each}}
<强> JS 强>
Template.myTemplate.helpers({
shouldShow: function (index) {
return (index + 1) % 2 === 0;
}
});