Control Flow & Loops
Control Flow Tags
Liquid syntax supports control flow tags which combined with operators, determine the conditions under which blocks of Liquid code (contained within the curly brace percentage delimiters) are executed.
As indicated in the Liquid documentation, Naxai supports the following sets of control flow tags:
- if, elsif, else, and endif
- unless, elsif, else, and endunless
- case, when, else, and endcase
if
Executes a block of code only if a certain condition is true. Use elsif to add multiple ‘if’ conditions and else for the block of code you want to execute if all conditions are false.
{% if contact.totalSpend > 0 %}
You deserve something special! Here's another 20% off!
{% else %}
Buy now! Would 5% off convince you?
{% endif %}
unless
The opposite of if – executes a block of code only if a certain condition is not met.
case/when
Creates a switch statement to execute a particular block of code when a variable has a specified value. Case initializes the switch statement and when statements define the various conditions.
A when tag can accept multiple values. When multiple values are provided, the expression is returned when the variable matches any values inside the tag. Provide the values as a comma-separated list, or separate them using an or operator.
An optional else statement at the end of the case provides code to execute if none of the conditions are met.
For example, you might use this to display different text depending on if a customer speaks a specific language.
{% case contact.language %}
{% when "fr" %}
Bonjour
{% when "es" %}
Hola
{% else %}
Hello
{% endcase %}
Loops
You can use iteration tags such as for, cycle and tablerow to output data that is an array type (a collection of values).
for
Repeatedly executes a block of code to perform an operation for each item in an array. You might use a for loop in a message to provide a receipt of purchases (assuming an array of items in a purchase). Find more information about for loops and iteration on the Liquid documentation.
{% for product in event.data.products %}
- {{ product.name }} x {{ product.price }}
{% endfor %}
Updated about 1 year ago