Blaze If Statement works only with Booleans?

Hey guys,

I am trying to use the Blaze {{#if}} statement to check whether a variable equals a string, but it seems like Blaze is only capable of simple true/false checks in if statements. Here is my code:

Template.Agenda.helpers({

        'event'(){

            const id = instance.state.get('id');
            return Events.findOne({_id:id});

        }

    });
{{#if event.status="Done"}}

<a class="waves-effect waves-light btn button-modal-white disabled">

{{else}}

<a class="waves-effect waves-light btn button-modal-white">

{{/if}}

It always shows me the option where the condition is true, regardless of what string I write (and without any string as well).

How can I get this string comparison done with Blaze without having to write one helper function for each?

Thanks!

Hi partick,

This plugin will help you acheive this result, currently it’s the only way to do this inside the template. They just include some global helpers so you don’t have to write them yourself.

{{#if $eq event.status "Done"}}

<a class="waves-effect waves-light btn button-modal-white disabled">

{{else}}

<a class="waves-effect waves-light btn button-modal-white">

{{/if}}
3 Likes

An alternative solution to that suggested by @francisbou is to create your own custom generic helpers. So, in your case, you’d do:

Template.registerHelper('equals', (a, b) => a === b);
1 Like