Benjamin Bohec
Benjamin Bohec

Reputation: 203

Meteor template each loop, provide other template each X results

Is it possible to insert specific data when meteor parse a collection with {{#each}} template logic only each X collection entry?

Exemple:

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

Results:

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 possible, what do I have to put on {{#if}} logic?

Thanks for your help.

Upvotes: 0

Views: 446

Answers (1)

David Weldon
David Weldon

Reputation: 64312

This is similar to this question. Give this a try:

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

Upvotes: 2

Related Questions