Handlebars.js if block helper ==

The easiest thing would be to add a custom if_eq helper:

Handlebars.registerHelper('if_eq', function(a, b, opts) {
    if(a == b) // Or === depending on your needs
        return opts.fn(this);
    else
        return opts.inverse(this);
});

and then adjust your template:

{{#if_eq this "some message"}}
    ...
{{else}}
    ...
{{/if_eq}}

Demo: http://jsfiddle.net/ambiguous/d4adQ/

If your errors entries weren’t simple strings then you could add “is this some message” flags to them and use a standard {{#if}} (note that adding a property directly to a string won’t work that well):

for(var i = 0; i < errors.length; ++i)
    errors[i] = { msg: errors[i], is_status: errors[i] === 'some message' };

and:

{{#if is_status}}
    <li>Status</li>
{{else}}
    <li>{{msg}}</li>
{{/if}}

Demo: http://jsfiddle.net/ambiguous/9sFm7/

Leave a Comment