在空格键中,如何在迭代{{# each}} 之间输入数据? [英] In Spacebars, how to enter data between iterations {{# each}}?
本文介绍了在空格键中,如何在迭代{{# each}} 之间输入数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 空格键 和 {{#each post}}
中,具体迭代后如何录入数据.例如在第二次迭代之后.
I'm using Spacebars and Meteor 1.2. In {{#each post}}
, how to enter data after a specific iteration. For example after the second iteration.
示例:1º 迭代 |2º 迭代 |数据 |3º 迭代 |4º迭代...
Example: 1º iteration | 2º iteration | data | 3º iteration | 4º iteration ...
推荐答案
首先,将光标转换为数组并添加索引属性.您还需要一个助手来检查相等性(或告诉您何时条件合适以显示不同的内容):
Firstly, convert your cursor to an array and add an index attribute. You're also going to need a helper to check equality (or to tell you when the conditions are right to display something different):
Template.myTemplate.helpers({
post: function(){
var cursor = Posts.find({}); // whatever your query is
var array = _.map(cursor, function(doc, index) {
doc.iteration = index + 1; // add an 'iteration' key starting at 1 instead of 0
return doc;
});
return array;
},
equals: function(a,b){ // determine equality of a and b for use in spacebars
return a==b;
}
});
然后在您的 html 模板中:
Then in your html template:
<template name="myTemplate">
{{#each post}}
Title: {{title}}
{{#if equals iteration 2}} Second iteration {{/if}}
{{/each}}
</template>
这篇关于在空格键中,如何在迭代{{# each}} 之间输入数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文