Usage: {{#if featured}}{{/if}}
{{#if}}
block helper comes built in with Handlebars.
{{#if}}
allows for testing very simple conditionals, and executing different template blocks depending on the outcome.
The conditionals that can be tested are very simple, essentially only checking for ’truthiness’. The evaluation rules are explained in the section below.
Like all block helpers, {{#if}}
supports adding an {{else}}
block or using ^
instead of #
for negation - this means that the {{#if}}
and {{else}}
blocks are reversed if you use {{^if}}
and {{else}} instead. In addition, it is possible to do {{else if ...}}
, to chain together multiple options like a switch statement.
false
, 0
, undefined
, null
, ""
(an empty string) or []
(an empty array) is considered false, and any other value is considered true.
0
evaluate to true, 0 is the same as falsefeatured
is a boolean flag. The following code example will evaluate to true only if the post is marked as featured.