【问题标题】:Having variables in Blaze Template for Meteor在流星的 Blaze 模板中具有变量
【发布时间】:2015-06-04 09:54:01
【问题描述】:
从 Meteor JS 中的 Template.helper 我得到一个数组,我可以使用它
{{#each array_result}}
{{value}}
{{/each}}
我想要的是
<table>
{{#each array_result}}
{{if count%4 ===0}}</tr><tr>
<td>{{value}}</td>
{{count++}}
{{/each}}
</table>
无论如何我可以在 HTML 中实现这一点。
【问题讨论】:
标签:
javascript
node.js
meteor
meteor-blaze
【解决方案1】:
您可以在帮助程序中迭代您的数组,并在将结果返回到模板之前以某种特殊方式标记每个第 4 项:
Template.myTemplate.helpers({
array_result: function() {
// fetch an array of docs/items somehow
var docs = SomeCollection.find().fetch();
// iterate over them and mark each 4th item as "awesome"
_.each(docs, function(doc, index) {
if (index % 4 === 0)
doc.isAwesome = true;
});
// return the modified documents
return docs;
}
});
您的模板可能如下所示:
<table>
{{#each array_result}}
{{#if isAwesome}}
...
{{else}}
...
{{/each}}
</table>
【解决方案2】:
谢谢@大卫。
我认为应该使用带有 CSS 的 Grids,它具有溢出功能并且比 HTML 表格具有响应性。