简体   繁体   English

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

[英]How can I use if condition on the meteor template?

I want to use an if condition in a Meteor Blaze template. 我想在Meteor Blaze模板中使用if条件。 Let's say you have a helper users on the Users collection you want to iterate through tasks and if the username is admin, use a "red" style: 假设您在Users集合上有一个帮助users ,您希望迭代任务,如果用户名是admin,则使用“红色”样式:

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

Meteor uses Spacebars , a variant of Handlebars , which are "logicless" templates. Meteor使用Spacebar作为Handlebars的一种变体,它是“无逻辑”模板。 You need to define a Template helper , then use it in the {{#if}} . 您需要定义模板助手 ,然后在{{#if}}使用它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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