Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor: Whats the best way to test equality of two values (like {{#if someVar == 'someVal'}}) in Blaze?

I'm having to define template helpers everywhere that simply test the equality of a document property with a constant so I can do something like this in my template:

    {{#if fruitIsPineapple}}...{{/if}}

And in my template helper looks like:

    Template.example.helpers({
      fruitIsPineapple: function () { return this.document.fruit === 'pineapple'; } 
    });

How can I save myself from having to create all these helpers? It'd be nice if there we an equality operator in Blaze...

like image 801
Patrick Canfield Avatar asked Mar 28 '14 06:03

Patrick Canfield


2 Answers

I had my question answered at the Meteor Devshop. Turns out you can define a Handlebars helper, like so:

    Template.registerHelper('equals', function (a, b) {
      return a === b;
    });

Then use it in prefix position like this:

    {{#if equals fruit 'pineapple'}}...{{/if}}
like image 86
Patrick Canfield Avatar answered Sep 28 '22 08:09

Patrick Canfield


Without any cumbersome code, you can achieve this by installing raix:handlebar-helpers and do something like this:

{{#if $eq a b}}
   ...
{{ /if }}
like image 44
jasenkoh Avatar answered Sep 28 '22 08:09

jasenkoh