Skip to main content

Conditionals

All conditional helpers that evaluate a condition and return a boolean value. Conditional helpers can be used in inline as well as in block format.

#and​

Truthy AND check for multiple values.

{{#and loggedIn verified}}
Welcome
{{else}}
Please verify your account
{{/and}}

Multiple values:

{{#and user email active}}
Complete profile
{{/and}}

Inline form:

{{and loggedIn verified yes="allowed" no="blocked"}}

#contains​

Returns true if the context as string contains the first parameter as string; otherwise returns false.

Inline form:

{{contains 'handlebars' 'bars'}} → true
{{contains 'handlebars' 'bars' yes='ON' no='OFF'}} → 'ON'
{{contains currencies 'EUR' }} → true if currencies is ['EUR', 'USD']
{{contains currencies 'EUR' }} → true if currencies is {'EUR': '€', 'USD': '$'}

Block form:

{{#contains 'handlebars' 'bars'}}
foo
{{else}}
bar
{{/contains}}

See the similar XPath 1.0 function.

This implementation also supports collections, arrays, and maps. For collections and arrays it will return true if the collection contains the parameter. For maps, it will return true if the map contains the parameter as a key. In all cases type (string, number, boolean, etc.) and value must match.

To make it XPath-strict, please use as {{contains x string(y)}}

#endsWith​

Returns true if the context as string ends with the first parameter as string; otherwise returns false.

Inline form:

{{endsWith 'abc' 'c' }} → true
{{endsWith 'abc' '1' }} → false
{{endsWith 'abc' '' }} → true

Block form:

{{#endsWith 'abc' '1' }}
YES
{{else}}
NO
{{/endsWith}}

#eq​

Tests equality.

{{#eq status "ACTIVE"}}
Active
{{else}}
Not active
{{/eq}}

Inline form:

{{eq status "ACTIVE" yes="active" no="inactive"}}

#gt​

Greater than. Values must be comparable.

{{#gt price 100}}
Expensive
{{else}}
Cheap
{{/gt}}

Inline form:

{{gt price 100 yes="expensive" no="cheap"}}

#gte​

Greater than or equal.

{{#gte stock 1}}
In stock
{{else}}
Out of stock
{{/gte}}

Inline form:

{{gte stock 1 yes="available" no="sold-out"}}

#lt​

Less than.

{{#lt age 18}}
Minor
{{else}}
Adult
{{/lt}}

Inline form:

{{lt age 18 yes="minor" no="adult"}}

#lte​

Less than or equal.

{{#lte discount 0}}
No discount
{{else}}
Discount available
{{/lte}}

Inline form:

{{lte discount 0 yes="no-discount" no="has-discount"}}

#matches​

Similar to #if but using a regular expression for evaluation. It has two options

  • safe: if it is set to false, exeption will be thrown (usefull during development/debugging). Default is true
  • fallback: if fallback="true", even the evaluation is false, the result still return true.

Block form:

{{#matches 'my-sku' '^[a-z0-9-]+$' }}
Vaklid SKU
{{else}}
Invalid SKU
{{/matches}}

Inline form:

{{matches 'my-sku' '^[a-z0-9-]+$' }} → true

If safe=false

{{matches 'my-sku' '^[' safe=false }} → throws HandlebarsException

Using fallback

{{matches 'my-sku' '^[' fallback=true }} → true

Even though regex is invalid "[", result is true.

#neq​

Tests inequality.

{{#neq role "admin"}}
Normal user
{{else}}
Admin user
{{/neq}}

Inline form:

{{neq role "admin" yes="normal" no="admin"}}

#not​

Negates truthiness.

{{#not deleted}}
Visible
{{else}}
Deleted
{{/not}}

Inline form:

{{not deleted yes="visible" no="deleted"}}

#or​

Truthy OR check for multiple values.

{{#or isAdmin isOwner}}
Can edit
{{else}}
Read only
{{/or}}

Inline form:

{{or isAdmin isOwner yes="can-edit" no="read-only"}}

#startsWith​

Returns true if the context as string starts with the first parameter as string; otherwise returns false.

Inline form:

{{startsWith 'abc' 'a' }} → true
{{startsWith 'abc' '1' }} → false
{{startsWith 'abc' '' }} → true

Block form:

{{#startsWith 'abc' '1' }}
YES
{{else}}
NO
{{/startsWith}}

See the similar XPath 1.0 function.