【问题标题】:How to use and condition in if statement on meteor template?如何在流星模板的if语句中使用和条件?
【发布时间】:2015-09-13 01:41:45
【问题描述】:

我想在 if 语句中使用 and 条件

{{#if requiredExp equals 0 and tile equals "SSE"}}
Expierince cannot be  equal to 0
{{/if}}

如何在meteor模板中使用and来写这个条件?

【问题讨论】:

  • 请注意,您可以使用模板条件的嵌套以及反转逻辑。您的第一个条件可以表示为{{#unless requiredExp}},只要requiredExp 的所有其他可能值都是true。 但是,第二个条件中的字符串比较需要一个帮助器。
  • 我回答了同样的问题here

标签: meteor


【解决方案1】:

Meteor 的设计方式使您无法在模板中添加逻辑。您可以创建一个简单的助手,如 equals,但不能使用 && 和 || 等逻辑运算符在模板中,因为这个逻辑不属于模板。

您可以为此创建一个全局 template helper 等于:

Template.registerHelper('equals', function(param1, param2) {
  return param1 === param2;
});

所以你可以在你的模板中使用它:

{{#if equals requiredExp 0}}
  {{#if equals tile "SSE"}} 
    Expierince cannot be equal to 0 
  {{/if}}
{{/if}}

或者,更好的选择是创建一个助手来处理您想要的逻辑:

Template.yourTemplateName.helpers({
  showExperienceNotification: function() {
    return this.requiredExp === 0 && this.tile === 'SSE';
  }
});

并在您的模板中使用它:

<template name="yourTemplateName">
  {{#if showExperienceNotification}}
    Experience cannot be equal to 0
  {{/if}}
</template>

您可以通过助手中的关键字this 访问模板数据: this.requiredExpthis.tile。阅读有关 Meteor 模板和数据上下文的更多信息:https://www.discovermeteor.com/blog/a-guide-to-meteor-templates-data-contexts/

【讨论】:

    猜你喜欢
    • 2015-04-24
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 2011-11-06
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    相关资源
    最近更新 更多