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

2

18

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

Timikatiming answered 28/3, 2014 at 6:15 Comment(2)
A package that defines a lot of handy helpers, including one for testing equality: github.com/raix/Meteor-handlebar-helpersTimikatiming
Why did someone downvote this? I had the same question, and the answer is not documented anywhere.Napiform
T
50

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}}
Timikatiming answered 28/3, 2014 at 6:15 Comment(3)
Doesn't it have to be UI.registerHelper now?Sideswipe
Shouldn't it be Template.registerHelper now? ;PPartnership
Sorry, I broke the up-counter from its {{equal up_counter 42}} state. \o>Deaminate
H
16

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

{{#if $eq a b}}
   ...
{{ /if }}
Hydrosol answered 28/4, 2015 at 14:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.