Skip to main content
All CollectionsEmailUsing the email composer
How to add display conditions to your email?
How to add display conditions to your email?

Learn how to condition the display of certain parts of your email according to your user data.

Cindie avatar
Written by Cindie
Updated over 2 weeks ago

From the Email Composer, display conditions can be added directly to the text blocks, structures, or containers that make up your templates.

These conditional blocks can be used to:

  • Define conditions for displaying text based on user data

  • Create a loop displaying the different elements present in an object array

⚠ Conditional blocks can only be activated on template structures or containers.

Conditional Block

To activate and add a condition to a structure/container:

  1. Click on the Edit email button in your campaign and then select the structure or container to which you want to apply the condition.

  2. In the menu on the left, click on Conditions

  3. Activate the toggle button Apply Display Conditions

A list of elements to fill in will then appear:

  • 1️⃣ Name (Required): Name of the condition (ex: display cart news items)

  • 2️⃣ Description (Optional): Description of the condition

  • 3️⃣ Code before module: Beginning of the condition (ex: {% if ... %})

  • 4️⃣ Code after module: End of condition (ex: {% endif %})

You can use the same condition on several structures/containers, use the same name and the other elements will be filled in automatically.

In the case of a condition in a text block, the > and < symbols are not supported on the Email Composer.

🚨 Modifying any element of the condition other than its name will modify all the conditions with the same name.


Type of conditions & data

Conditions

There are two possible types of condition, if and for:

if (only if)

These conditions allow a structure/container to be displayed or not, depending on the value of a user attribute or an event attribute.

For example, if you want to condition the display of your structure/container based on a boolean attribute indicating whether or not the user has a premium account:

Code before module

{% if is_premimum == true %} or {% if trigger_event.is_premimum == true %} (if the attribute is attached to the trigger event)

Code after module

{% endif %}

Several types of logical operators can be used in if conditions to compare the value of a user attribute:

  • ==: equal to

  • !=: not equal to

  • >: strictly greater than

  • <: strictly less than

  • =: greater than or equal to

  • <=: less than or equal to

if, else

The if and else structures can be used to manage a case and its opposite. If the if condition is satisfied, the first block of code is executed; otherwise, the else block is executed.

For example, if you want to display a different container/structure depending on a customer attribute indicating a promotion:

Block 1 (condition met):

Code before module

{% if has_discount == true %}

Code after module

No code

Block 2 (condition not met):

Code before module

{% else %}

Code after module

{% endif %}

It is also possible to link several if-else loops together to handle more complex cases:

Block 1 (condition 1 met):

Code before module

{% if condition1 == true %}

Code after module

No code

Block 2 (condition 1 not met, condition 2 met):

Code before module

{% else if condition2 == true %}

Code after module

No code

Block 3 (condition 1 and 2 not met):

Code before module

{% else %}

Code after module

{% endif %}

for (loop)

These conditions are used to retrieve data from object arrays. They will browse all the data present in all the objects in the array to display them in your structure/container.

For example, if you want to display the product information linked to the add_to_cart event, in the case where the array of objects has the name article_infos:

Code before module

{% for $item in trigger_event.article_infos %}

  • $item: Iteration variable you create that retrieves data from the object array for display on your structure/container in this way: item.product_name

  • trigger_event.article_infos: Indicates the name of the array of objects attached to the trigger event in which the data you want to retrieve can be found.

Code after module

{% endfor %}

Data

There are three types of user data:

  • Strings array []: Formerly called Tag collections, arrays can contain only a list of strings (String). You can only display:

    • All elements present in the array: {{ c.interests }}

    • The first element: {{ c.interests|first }}

    • The last element: {{ c.interests|last }}

  • Object {}: Objects make it possible to group several pieces of information that are linked in a single piece of user data together.

    For example, a user's address will no longer be divided into three string attributes (street, zipcode, city):

address : { 
"city":"Paris",
"zip_code":"75003",
"street":"43 rue Beaubourg"
}

You can refer to object properties using the following notation:

You live in {{ address.street }}, {{ address.zip_code }} {{ address.city }}

  • Object array [ {} ]: Object arrays are simply a list of objects. An array of objects can have up to three levels of nesting, i.e. an array of objects can contain an array of objects which itself contains an array of objects.

💡 Data in arrays of objects can only be accessed using a for loop.

The for loop will display all the objects in the array: it is not possible to choose which object to display.

This type of data is often found for retail customers attached to purchase validation or order confirmation events.

Here is an example of a table of purchase validation objects containing two products:

articles :[ 
{
"name":"Lord of the Rings",
"category":"film",
"price":14.99,
"quantity":1,
},
{
"name":"Brol Vinyle - Angèle",
"category":"music",
"price":31.99,
"quantity":1,
}
]


Some examples

Display condition for a block (if)

Boolean

Here is an example of a case where you want to display a block if the user is premium.

Context

There is an is_premium attribute attached to the user profile and you want to display the block if the value of this attribute is equal to true:

Code before module

There are several options:

  • Option 1: {% if is_premium %}

  • Option 2: {% if is_premium == true %}

Code after module

{% endif %}

String

Let's imagine that you want to display a block based on the user's region.

Context

You offer different baskets depending on the user's region, and the display depends on the value of the custom region_fr attribute:

Code before module

{% if region_fr =='Région Auvergne-Rhône-Alpes’ %}

Code after module

{% endif %}

Integer

Here, you want to display a block according to the number of loyalty points a user has.

Context

The fid_points attribute is of type int and you want to display the block if the user has more than 500 loyalty points:

Code before module

{% if fid_points >= 500 %}

Code after module

{% endif %}

Array of string (formerly Tag Collections)

Let's say you want to display a block based on the values in an array of strings (tag collections).

Context

You sell trackers for animals and a single customer can have several animals and therefore several trackers. You want to display the block if the string array favorite_category contains ‘hats’ or ‘sunglasses’:

Code before module

{% if t.favorite_category|contains('hats') or t.favorite_category|contains('sunglasses')%}

Code after module

{% endif %}

Condition for displaying a complex block (if, else if, else)

If you want to send an email containing a single piece of information, but adapted according to several conditions without making it too complicated to manage, using an if, else if, else block is ideal.

Let's imagine you want to send a personalized email to your customers, offering them trips based on their last trip or, failing that, their age:

Block 1:

Code before module

{% if last_purchased_trip == 'mountain' %}

Code after module

No code

Block 2:

Code before module

{% else if last_purchased_trip == 'beach' %}

Code after module

No code

Block 3:

Code before module

{% else if age >= 30 %}

Code after module

No code

Block 4:

Code before module

{% else %}

Code after module

{% endif %}

💡 This ensures that a single block is displayed while maintaining a flexible structure.

Adding a new condition, whether it relates to the same attribute or another, can be done without impacting the existing conditions.

Object personalization loop

The following use cases have been implemented for transactional email automations. These templates combine conditions and object personalization loops.

A single level of nesting

Here, let's say you want to display the list of items canceled for the user's order as follows:

{{item name}} {{item quantity}}       {{item price}}

Conditions:

Code before module

{% for $articles in trigger_event.order %}

Code after module

{% endfor %}

Data display in the template:

{{$articles.product_name}} x {{$articles.quantity}} {{$articles.total_price}}

Two levels of nesting

Here is an example of an order validation email automation, which is based on the validated_order event composed of an array of objects with two levels of nesting:

  • 1st level: array of orders objects containing order information

  • 2nd level: array of product objects containing product information

Here, orders can be split up at different times, as the products ordered by a user can be sent by several shippers. In the case of multiple shippers, you would like the orders to be sent one after the other.

💡 Split command: is_splitted attribute set to true

The template will contain two structures, the first with the order information and the second with the products in the orders. As a result, the display condition will be spread over both structures.

1st structure

Code before module

  • {% if trigger_event.is_splitted %}

  • {% for $order in trigger_event.orders %} (we browse the array of orders)

Code after module

No code

2nd structure

Code before module

{% for $articles in $order.product %} (we browse the array of lines objects present in the array of orders objects)

Code after module

  • {% endfor %} (closes the for loop initialized in the 2nd structure)

  • {% endfor %} (closes the for loop initialized in the 1st structure)

  • {% endif %} (closes the if condition initialized in the 1st structure)


Testing the conditions

To test your display conditions in real conditions, you can click on your email, select ‘...’ then ‘preview as’: 

Then, add a Custom ID and click on 'Update preview':

The content of your email will appear with the user's personal details in place of the conditions.

Note that the email address you want to preview must be associated with a profile that has values for the conditions to be tested.

You can then test how the email looks, by using the Send test button on the email message window and typing your email address:

The email is immediately sent, with the right conditions applied! ✨


This article belongs to Batch's FAQ. Need more help? Find insightful articles, documentation, case & market studies, guides, and even more in our website's Resources section on batch.com and our blog.

Did this answer your question?