meteor:模板中的块循环

时间:2015-08-23 06:21:28

标签: javascript meteor

我的流星模板的重量是同一元素的五倍。所以我认为它应该能够循环它:

<table class="article" data-id="{{../article._id}}" data-section="something">
    <thead>
        <tr>
            <th>Something</th>
        </tr>
    </thead>
    <tbody>
        {{#each something}}
            <tr>
                <td><input type="text" value="{{title}}" name="title"></td>
            </tr>
        {{/each}}
    </tbody>
</table>

<table class="article" data-id="{{../article._id}}" data-section="anything">
    <thead>
        <tr>
            <th>Anything</th>
        </tr>
    </thead>
    <tbody>
        {{#each anything}}
            <tr>
                <td><input type="text" value="{{title}}" name="title"></td>
            </tr>
        {{/each}}
    </tbody>
</table>

正如你所看到的,唯一的区别是数据元素(某事物/任何事物),th元素(Something / anything)和每个元素(某事物/任何事物)。这样共有五张桌子。我如何制作它的循环?

1 个答案:

答案 0 :(得分:0)

您需要一个或两个模板和两个辅助函数。我会用两个模板来做,因为它更清楚:

HTML:

<template name="outer">
  {{#each theFive}}
    <!-- ../article._id is referring to the data context of the parent of this template
    in reality you'll probably use ../this._id -->
    <table class="article" data-id="{{../article._id}}" data-section="{{someOuterThingKey}}">
      <thead>
        <tr>
          <th>{{someOuterThingKey2}}</th>
        </tr>
      </thead>
      <tbody>
        {{#each something}}
          {{> inner}}
        {{/each}}
      </tbody>
    </table>
  {{/each}}
</template>

<template name="inner">
  <tr>
    <td><input type="text" value="{{title}}" name="title"></td>
  </tr>
</template>

JS:

Template.outer.helpers({
  theFive: function(){
    ...your code that builds the return variable
    return aCursorOrArrayWithTheFiveOuterThings;
  }
});

Template.inner.helpers({
  something: function(){
    ...your code that builds the return variable
    return aCursorOrArrayWithTheInnerThings;
  }
});