Use conditionals in messages
Learn how conditional tags give you powerful controls for personalization in your messaging.
There are two types of conditionals that are particularly useful in Klaviyo:
- If/else conditionals
These show an element (e.g., a word, phrase, or block) if a recipient meets certain criteria you set. You can optionally include a series of conditions with different content for each one. - For conditionals
These iterate through data formatted as a list and show an element repeatedly for each list entry.
If-else conditionals
If statements allow you to control the content someone receives based on profile or event data.
In the example below, a person will be shown the first line of text if they have more than 150 loyalty points. If they have less than 150, but more than 0, they will see the second line. And if they have no loyalty points, they will see the third message.
{% if person|lookup:'Loyalty Points' > 150 %}
Hey VIP! You’ve always got free shipping & free returns
{% elif person|lookup:'Loyalty Points' > 0 %}
You have {{ person|lookup:'Loyalty Points' }} points, and you just need 150 to become a VIP!
{% else %}
Have you heard about our VIP program? Join today on our website to start earning rewards.
{% endif %}
Within an if statement, an initial {% if … %}
condition is required, as well as the closing {% endif %}
tag. You can use the other elements (i.e., {% elif %}
and {% else %}
tags) to create more complex if statements, if your use case requires it. You can use an unlimited number of {% elif %}
tags, followed by a maximum of one {% else %}
tag. Each email recipient will only see the content from the first condition they qualify for, so consider the order of your if
and elif
conditions.
Condition structures
Within your if
statement, there are different ways to set up your condition based on how your data is stored (i.e., as text, numbers, lists, or booleans) and what criteria you’d like to use.
All available condition structures are outlined below, including the data types they are compatible with. These conditions can be used within if or elif tags (i.e. {% if CONDITION %}
or {% elif CONDITION %}
).
Note that these conditions are case-sensitive.
Sample condition | Show the block if... | Acceptable data types |
---|---|---|
person|lookup:'Favorite Color' | The Favorite Color property is set (has any value) | Any |
not person|lookup:'Favorite Color' | The Favorite Color property is not set (does not exist on the profile, or is empty) | Any |
person|lookup:'Favorite Color' == 'green' | The Favorite Color property has the value green | Text, Number |
person|lookup:'Favorite Color' != 'green' | The Favorite Color property does not have the value green | Text, Number |
person|lookup:'Age' > 20 | The Age property contains a number greater than 20 | Number |
person|lookup:'Age' >= 20 | The Age property contains a number greater than or equal to 20 | Number |
person|lookup:'Age' < 20 | The Age property contains a number less than 20 | Number |
person|lookup:'Age' <= 20 | The Age property contains a number less than or equal to 20 | Number |
'green' in person|lookup:'Favorite Colors' | The property Favorite Colors contains a list, and green is one of the list items, OR The property Favorite Colors contains text, and ‘green’ exists anywhere in the text | |
not 'green' in person|lookup:'Favorite Colors' | The property Favorite Colors contains a list, and green is not one of the list items, OR The property Favorite Colors contains text, and green does not exist anywhere in the text | List, Text |
Conditions for booleans
If you are referencing data stored as a boolean, you’ll need to use 1 and 0 rather than “true” and “false” in your condition definition. Do not surround the 1 or 0 in quotes. Use the sample conditions below as a template.
Sample condition | Show the block if... |
---|---|
`person | lookup:'VIP' == 1` |
`person | lookup:'VIP' == 0` |
Conditions for booleans stored as text
If your true/false data is stored as text, not as a boolean, use the sample conditions for text properties above. If you aren’t sure, or if you are referencing a property that contains both booleans and text, you can use these structures to cover all scenarios. Include all spellings and capitalizations that are present in your data.
Sample condition | Show the block if... |
---|---|
`person | lookup:'VIP' == 1 or person |
`person|lookup:'VIP' == 0 or person|lookup:'VIP' == 'false' or person|lookup:'VIP' == 'False' | The VIP property is set to the boolean value false or the strings false or False |
For conditionals
For
tags allow you to iterate over each item in a variable that stores a list and render them individually. Below is an example statement:
Sample code | Result |
---|---|
{% for item in event.shopping_cart_items %}{{ item.name }} × {{ item.quantity }} {% endfor %} | Oversized Beach Blanket × 1 Beach Chairs × 430 SPF Sunscreen × 220 Plastic Cooler × 1 |
Each for
statement must contain the following:
- An opening
{% for … %}
tag, containing a row alias (item in the example above) and row collection (event.shopping_cart_items in the example above) - A closing
{% endfor %}
tag
Between the two required tags, you can include any text you’d like. To include variables nested within the row collection, replace the beginning of the variable name (the row collection plus the number following the row collection) with your row alias. For example, the variable {{ event.shopping_cart_items.0.name }}
would become {{ item.name }}
, and {{ event.shopping_cart_items.0.quantity }}
would become {{ item.quantity }}
.
If you’d prefer to show only a set number of items from a list, apply the slice
filter to the row collection. Using the example above, {% for item in event.shopping_cart_items|slice:':3' %}
would result in only the first three items on the list displaying (regardless of how many items were on the list). If the list contains three or fewer items, all items in the list will appear once.
For loops
To customize how your for
conditional treats the first or last item in your list, use the forloop.first
or forloop.last
variable.
For example, forloop.last
can be used to create a comma-separated list where the word “and” is added between the final two items like this:
Sample code | Result |
---|---|
You need to buy {% for item in event.grocery_list.items %}{% if not forloop.last %}{{ item.name }}, {% else %}and {{ item.name }}{% endif %}{% endfor %}. | You need to buy milk, bread, cheese, and eggs. |
Updated 2 months ago