Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"}}
like image 575
Dariusz Sikorski Avatar asked Jun 07 '15 10:06

Dariusz Sikorski


2 Answers

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}}
like image 79
saimeunt Avatar answered Nov 01 '22 05:11

saimeunt


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}}
like image 9
Adnan Y Avatar answered Nov 01 '22 04:11

Adnan Y