最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Meteor templates, check if value equals string - Stack Overflow

programmeradmin1浏览0评论

Here's the template structure

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

Is that possible to view only items with given value? And the second question, how to combine this two statements:

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

Here's the template structure

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

Is that possible to view only items with given value? And the second question, how to combine this two statements:

{{#if future}} {{#if timezone="Europe/Warsaw"}}
Share Improve this question asked Jun 7, 2015 at 10:10 Dariusz SikorskiDariusz Sikorski 4,4075 gold badges30 silver badges44 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 9

You can create a dedicated helper to check if a timezone is equal to a certain value :

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

If you want to combine two Spacebars {{#if}} block helpers, once again create a dedicated helper that performs the test in JS :

JS

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

HTML

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

Use Template.registerHelper to create a global helper. For instance, to create a helper that compares two arbitrary variables:

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;
  }
});

Then use it using:

{{#if compare timezone "Europe/Warsaw"}}
     // Do something
{{/if}}
发布评论

评论列表(0)

  1. 暂无评论