conditionally show information in templates using if, unless, and else helpers.


Handlebar templates include if else and unless conditional helpers. These helpers can be invoked as block or inline helpers.

Block Usage

Conditional statements can be invoked in block format to show blocks of HTML if the conditional statement evaluates to true. In the example below, the severity property will only be displayed if it is present.

   <span>Severity: {{}}</span>

The {{if}} helper tests for truthiness. This means that the condition will be true except when it is false, undefined, 0, [], or null.

The inverse of the if helper is the unless helper.

   <span>No Severity Level Found</span>

Both the if helper and unless helper can use the else helper.

    <span>Severity: {{}}
    <span>No Severity Level Found</span>

Inline Usage

Conditional helpers can also be used inline. When using the helper inline, no # sign is needed.

    {{if "Active" "Not Active"}}

In the example above, the <div> will display the string Active if the boolean value isActive is true. Otherwise, if isActive is false the string Not Active will be displayed.

Inline conditional helpers can be used to conditionally add CSS classes to your template.

<span class={{if "green" "red"}}></span>

Oftentimes you will combine this inline conditional usage with truth helpers.

<span class={{if (gt 50) "red" "green"}}>

In the example above, the severity level will be displayed using the red CSS class if the severity value is greater than 50, otherwise the severity level will be displayed using the green CSS class. These CSS classes might set the color attribute on the text.

For more detailed information on conditional helpers please see the Ember handlebars guide located here

Last updated