流星模板,检查值是否等于字符串

时间:2015-06-07 10:10:39

标签: javascript templates if-statement meteor

这是模板结构

{{#each loadedEvents}}
  {{#if future}}
    {{#if timezone="Europe/Warsaw"}}
    {{> event}}
  {{/if}}
{{/each}}

是否可以仅查看具有给定值的项目? 第二个问题,如何结合这两个陈述:

{{#if future}} {{#if timezone="Europe/Warsaw"}}

2 个答案:

答案 0 :(得分:8)

使用Template.registerHelper创建全局帮助程序。例如,要创建一个比较两个任意变量的帮助器:

Template.registerHelper('compare', function(v1, v2) {
  if (typeof v1 === 'object' && typeof v2 === 'object') {
    return _.isEqual(v1, v2); // do a object comparison
  } else {
    return v1 === v2;
  }
});

然后使用:

{{#if compare timezone "Europe/Warsaw"}}
     // Do something
{{/if}}

答案 1 :(得分:8)

您可以创建专用帮助程序来检查时区是否等于某个值:

Template.loadedEvents.helpers({
  timezoneIs: function(timezone){
    return this.timezone == timezone;
  }
});

如果你想组合两个空格键{{#if}}块帮助器,再次创建一个专用的帮助程序,在JS中执行测试:

JS

Template.loadedEvents.helpers({
  isFutureAndTimezoneIs: function(timezone){
    return this.future && this.timezone == timezone;
  }
});

HTML

{{#each loadedEvents}}
  {{#if isFutureAndTimezoneIs "Europe/Warsaw"}}
    {{> event}}
  {{/if}}
{{/each}}