2016-04-01 9 views
0

{{#each}}テンプレートロジックを使ってコレクションを解析する際に、特定のデータを挿入することは可能ですか?Meteorテンプレート各ループ、各Xの結果を別のテンプレートで提供

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

結果:

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ロジックに入れなければならないのですか?

ありがとうございました。

答えて

2

this questionと似ています。これを試してみる:これだけです

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

パーフェクト。私はちょうど私のコレクションの各Xエントリだけの値を表示するために、jsテンプレートヘルパーで%の後の数字を変更する必要があります。 –

+0

ええ、 '{{#if shouldShow @index 3}}'のように2番目のパラメータを使用して、3番目ごとにそれを実行することもできます。そうすれば、単一のヘルパーを複数の場所で使用することができます。 –

関連する問題