Articles in this section

#and helper

The #and block helper renders one expression if both arguments have a non-empty value, and a fallback {{else}} expression if either argument is empty, null, or undefined. Note that the integer 0 is treated as absent even though it has a value -- but the string "0" is treated as present because it's a non-empty string.

Syntax

{{#and field1 field2}} expr {{else}} expr {{/and}}

field1, field2 -- Two fields or values to evaluate. Both must be truthy for the block to render. Required.

Examples

Render output when both fields are present

{{#and legends.unicorns legends.horses}}
 {{legends.unicorns}} - {{legends.horses}}
{{else}}
 Not found
{{/and}}

Input:

{
 "legends": {
 "unicorns": "11",
 "ponies": "22",
 "horses": "33",
 "total": "66"
 }
}

Output: 11 - 33

Render a full name when first and last name are both present

{{#and firstName lastName}}
 {{firstName}} {{middleName}} {{lastName}}
{{else}}
 Not found
{{/and}}

Input:

{
 "firstName": "Hillary",
 "middleName": "Ann",
 "lastName": "Swank"
}

Output: Hillary Ann Swank

Falsy values that cause the {{else}} branch to render

Value Type Evaluates as
0 Integer absent (empty)
"" Empty string absent (empty)
null Null absent (empty)
undefined Undefined absent (empty)
"0" String present (non-empty)

Usage notes

  • Both arguments must have a non-empty value for the block to render. If either is empty, null, or undefined, the {{else}} expression renders instead.
  • The integer 0 is treated as absent even though it has a value. The string "0" is treated as present because it's a non-empty string.
  • The {{else}} clause is optional but recommended to handle missing or null field values gracefully.
  • Fields referenced inside the block (e.g. {{middleName}} in the name example) don't need to have a non-empty value -- only the two arguments passed to #and are evaluated.