How to do an "if equals" conditional comparison in Handlebars

Last reviewed in June 2019 with Ember Octane

If you ever used the if helper, you have likely tried to do a conditional comparison. For example: “render OK only if the model status equals complete”:

{{#if model.status == "complete"}}  {{! doesn't work }}
  OK
{{/if}}

As you may have figured out, this does not work. The if helper can only test for properties to be true or false – not arbitrary expressions.

Enter Handlebars subexpressions:

{{#if (eq model.status "complete")}}  {{! works! }}
  OK
{{/if}}

What is eq you may ask?

It's a helper, available with the Ember Truth Helpers add-on which implements many logical/comparison operators.

Here's how we install it:

$ ember install ember-truth-helpers

Ember Composable Helpers is another fantastic add-on you may find extremely useful in your Ember toolbox.

New to Ember? Start here!

Getting Started with Ember Octane

Enjoyed this article? Join Snacks!

Snacks is the best of Ember Octane in a highly digestible monthly newsletter. (No spam. EVER.)