简体   繁体   中英

Having variables in Blaze Template for Meteor

From the Template.helper in Meteor JS I get an array which I can use by

{{#each array_result}}
  {{value}}
{{/each}}

What I want is

<table>
  {{#each array_result}}
    {{if count%4 ===0}}</tr><tr>
      <td>{{value}}</td>
      {{count++}}
  {{/each}}
</table>

Is there anyway I can achieve this in the HTML.

You can iterate over your array in your helper and mark each 4th item in some special way before returning the result to the template:

Template.myTemplate.helpers({
  array_result: function() {
    // fetch an array of docs/items somehow
    var docs = SomeCollection.find().fetch();

    // iterate over them and mark each 4th item as "awesome"
    _.each(docs, function(doc, index) {
      if (index % 4 === 0)
        doc.isAwesome = true;
    });

    // return the modified documents
    return docs;
  }
});

Your template could then look something like this:

<table>
  {{#each array_result}}
    {{#if isAwesome}}
      ...
    {{else}}
      ...
  {{/each}}
</table>

Thanks @David.

I believe one should use Grids with CSS which has overflow feature and are responsive than HTML tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM