In Spacebars, how to enter data between iterations {{# each}}?

主宰稳场 提交于 2019-12-23 22:04:08

问题


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.

Example: 1º iteration | 2º iteration | data | 3º iteration | 4º iteration ...


回答1:


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;
  }
});

Then in your html template:

<template name="myTemplate">
  {{#each post}}
    Title: {{title}}
    {{#if equals iteration 2}} Second iteration {{/if}}
  {{/each}}
</template>


来源:https://stackoverflow.com/questions/32800524/in-spacebars-how-to-enter-data-between-iterations-each

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!