Articles in this section

#unless helper

The #unless block helper renders one expression when a field is empty, null, or undefined, and an optional {{else}} expression when the field has a non-empty value. It's the inverse of the #if helper -- use it when it's more natural to write the condition as "render this if the field is absent."

Syntax

{{#unless field}} expr {{else}} expr {{/unless}}

field -- The field or value to evaluate. Required. If empty, null, or undefined, the main block renders. If the field has a non-empty value, the {{else}} block renders.

Examples

Show a fallback when a field is missing

{{#unless contact.phone}}
  Phone number missing
{{else}}
  Contact phone number is:
  {{contact.phone}}
{{/unless}}

When contact.phone is absent:

Input:

{
  "contact": {
    "street": "streetName",
    "address": "streetAddress"
  }
}

Output: Phone number missing

When contact.phone is present:

Input:

{
  "contact": {
    "phone": "310-555-1234",
    "street": "streetName",
    "address": "streetAddress"
  }
}

Output:

Contact phone number is:
310-555-1234

Usage notes

  • #unless is the inverse of #if: it renders the main block when the field is absent, and the {{else}} block when it's present.
  • Values treated as absent: null, undefined, empty strings, and the integer 0.
  • The {{else}} clause is optional. If omitted and the field has a non-empty value, the helper renders nothing.
  • When the logic reads more naturally as "render this if the field is present," use the #if helper instead.