如何在流星模板上使用if条件? [英] How can I use if condition on the meteor template?

查看:56
本文介绍了如何在流星模板上使用if条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to use an if condition in a Meteor Blaze template. 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:

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


推荐答案

Meteor使用Spacebars Handlebars 的变体,这是无逻辑模板。您需要定义模板助手,然后在中使用它{{# if}}

Meteor uses Spacebars, a variant of Handlebars, which are "logicless" templates. You need to define a Template helper, then use it in the {{#if}}.

Template.foo.helpers({
  isAdmin: function (name) {
    return name === "admin"
  }
});



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

这篇关于如何在流星模板上使用if条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆