0

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.

Benjamin Bohec
  • 118
  • 2
  • 11

1 Answers1

2

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;
  }
});
David Weldon
  • 63,632
  • 11
  • 148
  • 146
  • Perfect that's it. I just have to change the number after % in js template helper in order to show the value only each X entries of my collection. – Benjamin Bohec Apr 01 '16 at 18:17
  • Yeah, you can also use a second parameter - like `{{#if shouldShow @index 3}}` could do it every 3rd one. That way you could have a single helper and use it in multiple places. – David Weldon Apr 01 '16 at 18:20