Dariusz Sikorski
Dariusz Sikorski

Reputation: 4407

Meteor templates, check if value equals string

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"}}

Upvotes: 7

Views: 7316

Answers (2)

saimeunt
saimeunt

Reputation: 22696

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

Upvotes: 9

Adnan Y
Adnan Y

Reputation: 3230

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

Upvotes: 9

Related Questions