2

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 ...

Cláudio Júlio
  • 365
  • 4
  • 16
  • 1
    This is very similar to [this question](http://stackoverflow.com/questions/28626410/creating-a-numbered-list-for-meteor-data) with the added twist of wanting to do something extra on a specific iteration. You can have a template helper that compares the iteration number to your target and that way your template can add some extra content at specific iterations. – Michel Floyd Sep 27 '15 at 00:18

1 Answers1

2

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>
Michel Floyd
  • 18,793
  • 4
  • 24
  • 39