Logic/conditional statements

Logic helpers allow you to place conditional requirements in Handlebars expressions.

Logic statements can use one or more of these properties to evaluate a condition:

  • value — A literal string or number (e.g. “Hello” or 3)
  • attribute — The string ID/Name of a user-specific data
  • expression — A helper that can use other helpers, attributes, and values to return a value

If/Else

if, else, else if — If/else statements can conditionally render blocks of content. If and else if statements render content when the condition is true, and else renders content when all previous conditions in a block expression are false.

Use if explicitly to test that a variable exists, is not empty, or is not 0. Otherwise, any condition that evaluates a variable acts as an implicit if statement.

For example, you must explicitly declare if when testing whether or not the fav_movie variable exists, is not empty, and is not 0.

{{#if fav_movie}}
   Your favorite movie is {{fav_movie}}.
{{else}}
   Maybe you'd enjoy a book instead.
{{/if}}

When testing the value of the fav_movie variable, you can use other logic helpers such as Equal and combine them with else statements.

{{#eq fav_movie "Indiana Jones"}}Did you like The Last Crusade or Temple of Doom?
{{else eq fav_movie "Star Wars"}}Are you into the OG trilogy or are you a prequel person?
{{else}}What's your favorite book?{{/eq}}

You can chain multiple else statements — similar to else if statements in other languages — to test a series of variables. Generally, the last else statement contains no parameters and determines what happens if all previous conditions fail.

And/Or

and, or — These operators help you string together complex conditions to show the right content to the right members of your audience.

 Note

and renders a block of content if all conditions are true.

or renders a block of content if any of the conditions are true.

FormatExampleOutput
{{#and expression expression}}{{#and (eq genre "action") (eq era "80s") }}<br/>You will definitely like Indiana Jones!<br/>{{/and}}If all conditions are true, “You will definitely like Indiana Jones!” will display
{{#or expression expression}}{{#or (eq genre "action") (eq era "80s") }}<br/>You might be interested in Indiana Jones.<br/>{{/or}}If any conditions are true, “You might be interested in Indiana Jones. will display

Equal

eq — Equality operators let you test the value of a merge field or the result of an expression — whether it is equal to, or not equal to, specific value.

 Tip

Equality operators work well with both numeric or string (text) values. But cannot compare between the two value types.

In this example, the message is different if the category merge field contains the value Accessories:

{{#eq category "Accessories"}}
   Enjoy up to $25 off!
{{else}}
   Get up to $75 off!
{{/eq}}
FormatExampleOutput
`{{#eq ## Equal

eq — Equality operators let you test the value of a merge field or the result of an expression — whether it is equal to, or not equal to, specific value.

 Tip

Equality operators work well with both numeric or string (text) values. But cannot compare between the two value types.

In this example, the message is different if the category merge field contains the value Accessories:

{{#eq category "Accessories"}}
   Enjoy up to $25 off!
{{else}}
   Get up to $75 off!
{{/eq}}
FormatExampleOutput
{{#eq expression expression}}{{#eq quantity 2}}You have 2 items in your cart{{/eq}}Displays text if quantity is equal to 2

Not equal

neq — Renders content when a merge field or the result of an expression is not equal to a value.

FormatExampleOutput
{{#neq expression expression}}{{#neq movie "Looper"}}You answered incorrectly. The correct answer is Looper{{/neq}}Display text if movie does not equal Looper

Not

not — Renders content if a merge field or the result of an expression does not exist, is empty, is false, or is 0. This is a direct way to test for the availability of a merge field.

FormatExampleOutput
{{#not expression}}{{#not watch_queue}}Your queue is empty, add a show to watch.{{/not}}If watch_queue is empty or 0, “Your queue is empty, add a show to watch.” will display

Unless

unless — Renders content in a template if a condition is false — like a reverse if condition. You might want to use unless when you only really need the else statement for a condition.

FormatExampleOutput
{{#unless expression}}{{#unless dislikes_indiana_jones}}You'll probably like Indiana Jones{{/unless}}Airship will render message content if the condition dislikes_indiana_jones is empty or false.

Greater than and Less than

gt, lt, gte, lte — Use the following operators to show content if the value of a variable — or the length of an array — is greater than, less than, greater than or equals, or less than or equals a particular value.

 Tip

These operators are for numerical values only.

FormatExampleOutput
{{#gt expression}}{{#gt quantity 2}}You have more than 2 items in your cart{{/gt}}Displays text if quantity is greater than 2
{{#lt expression}}{{#lt quantity 2}}You have less than 2 items in your cart{{/lt}}Displays text if quantity is less than 2
{{#gte expression}}{{#gte quantity 2}}You have enough items in your cart to unlock our discount{{/gte}}Displays text if quantity is greater than or equal to 2
{{#lte expression}}{{#lte quantity 2}}You don't have enough items in your cart to unlock our discount{{/lte}}Displays text if quantity is less than or equal to 2
 Note

Array lengths are 0-indexed. For example, if an array has 3 entries, {{#gte field.length 2}} will be true.