如何在流星模板上使用 if 条件?

How can I use if condition on the meteor template?

我想在 Meteor Blaze 模板中使用 if 条件。假设您在 Users 集合上有一个助手 users,您想要遍历任务,如果用户名是 admin,请使用 "red" 样式:

<ul>
    {{#each users}}
        <li {{#if(name==admin)}}class="red"{{/if}}>{{name}}</li>
    {{/each}}
</ul> 

Meteor 使用 Spacebars, a variant of Handlebars, which are "logicless" templates. You need to define a Template helper,然后在 {{#if}} 中使用它。

Template.foo.helpers({
  isAdmin: function (name) {
    return name === "admin"
  }
});
<ul>
  {{#each users}}
    <li {{#if isAdmin name}}class="red"{{/if}}>{{name}}</li>
  {{/each}}
</ul>