The #if block helper renders a block of content when a condition is truthy, and optionally renders an alternative block with {{else}} when the condition is falsy. Builders use it in Handlebars templates — including field mappings, response mappings, HTTP request bodies, and dynamic API paths — to include content only when a field has a value, or to provide a fallback.
Handlebars treats the following as falsy: false, undefined, null, "" (empty string), 0, and [] (empty array). When the condition is any of these, #if renders the {{else}} block if one is present, and otherwise renders nothing.
Usage
{{#if field}}contentIfTruthy{{else}}contentIfFalsy{{/if}}
field -- The value or field reference to evaluate. The helper renders the first block when this value is truthy. Required.
The {{else}} block is optional. Without it, the helper renders nothing when the condition is falsy.
Don't add extra spaces inside the expression. Handlebars renders any whitespace between blocks literally, so unintended spaces can appear in your output.
Related conditional helpers and keywords
| Helper or keyword | Description |
|---|---|
#if |
Renders its block when the condition is truthy. |
{{else}} |
Renders an alternative block when the condition is falsy. |
else if |
Tests a new condition when the previous condition is falsy. |
#compare |
Compares two values with an operator and renders its block when the comparison is true. |
#each |
Iterates over an array or object, rendering its block once for each item. |
Examples
Render a field when it has a value
Use #if on its own to include content only when a field is truthy.
{{#if data.name}}{{data.name}}{{/if}}
This renders data.name only when it has a value.
Provide a fallback with else
{{#if Name}}{{Name}}{{else}}{{/if}}
This renders Name when it has a value. When Name is falsy, the empty {{else}} block renders nothing, which outputs the value or an empty string.
Compare two values
Use the #compare helper to render content based on a comparison between two values.
{{#compare field operator field}}expr{{else}}expr{{/compare}}
Replace operator with a comparison operator, such as == or !=.
Iterate over a list
Use the #each helper to render a block once for each item in an array or object.
{{#each data.identity-profiles}}{{vid}}{{/each}}
This renders the vid value for every item in data.identity-profiles.
Combine #if and #compare
Nest #compare inside #if to act only when a field exists and also meets a comparison. This example renders data.name only when it exists and isn't an empty string.
{{#if data.name}}{{#compare data.name "!=" ""}}{{data.name}}{{else}}{{/compare}}{{/if}}