Customize your emails with "IF" conditions

In this article, we will explain how to personalize your emails with if conditions

❗️ Important
This article is dedicated for an advanced use of conditional display, otherwise you can use the the built-in Conditional display/Block visibility feature or Advanced personalization conditions

What are if conditions?

if conditions is the language used in our Conditional display/Block visibility features on the Drag & Drop Editors. It allows to Show or hide blocks based on contact attributes, transactional parameters or dynamic lists.

The powerful logic offered by if conditions enables you to add or remove entire design blocks within an email campaign or template, or modify specific content within a block depending on the conditions you set. You can use if conditions in two different ways:

  • Simple if conditions can be quickly and easily applied to individual design blocks in the Drag & Drop Editor (Classic) using the built-in Conditional display feature. 
    if_conditions_editor_EN.png
  • Custom if statements can be built and placed directly within the content of your email campaign or template.
    mceclip1.png

Using if conditions effectively extends the usefulness of a single email for multiple scenarios by displaying different sets of content or text based on:

  • contact attributes defined in your contact list
  • parameters passed in your API call

Common uses for if conditions 

There are countless ways to use if conditions to personalize the content of an email, but some of the most common are: 

  1. an email greeting, such as Hello Mr. Smith, Hello Mrs. Brown or simply Hello for contacts without gender or name data on file.
  2. a product or promotional image, such as promoting an image of a model that wears the same clothing or size as your recipient, or displaying the featured item in their favorite color.
    If_conditions_email-2_EN.png  If_conditions_email-1.png
  3. a promotional message, such as including an extra marketing offer in your transactional emails based on their purchases or other attributes.
    If_conditions_email-3_EN.png  Email_3.png

Structure of an if statement

💡 Good to know
We do not recommend applying if conditions to variables that contain “float” values as they may not produce accurate results. However, these may be applied if the value is passed as a string (contained in quotation marks “like this”).

Main rules to follow

Here are some rules you must follow when building your own if statement:

  • An if statement always starts with an if tag containing a condition: {% if ... %}
  • Optionally, the if statement may contain alternative conditions which will be evaluated sequentially. These alternative conditions start with: {% elif ... %}
  • Also on an optional basis, a catch-all clause can be added just before the end of the if statement, like this: {% else %}
  • An if statement always ends with: {% endif %}

Common ways to structure an if statement

Here are several common ways to structure an ifstatement:

if

Checks if a value is true or if an array is not empty

{% if contact.ACTIVE %}
Congrats! You achieved your goal this month.
{% endif %}

---

{% if params.tutors %}

{% for tutor in params.tutors %}
The following tutors are available to help you:
<ol>
<li>{{ tutor.name }}</li>
{% endfor %}
</ol>
{% endif %}

==

Checks if an expression is true

{% if coupon == “WELCOME” %}
<p>Welcome to our list! Here’s your first coupon: WELCOME25</p>
{% endif %} 

---

{% if contact.DONOR == true %}
<b>Thank you for your support!</b>
{% endif %}

if, in

Checks if a value (substring) exists within a string or if a variable exists within array

{% if “@example.com" in "bob@example.com" %}
This appears since "@example.com" is a substring of "bob@example.com"
{% endif %}

---

{% if "Piano" in params.types %}
Be careful! There are heavy items in your order. You must be present to receive this delivery.
{% endif %}

not

check for values that are false

{% if not user.subscribed %}
   <p>You are not subscribed to our secret sale alerts. Sign up here.</p>
{% endif %}

and / or

Evaluates multiple conditions

{% if temperature > 10 and temperature < 55 %}
   <p>Brr. It’s cold! Here’s a coupon for 20% off of any hot beverage, today only.</p>
{% endif %}

---

{% if contact.LANG == “FR” and contact.COUNTRY == “Canada” %}
{% endif %}

---

{% if contact.COUNTRY == “United States” or contact.COUNTRY == “Canada” %}
{% endif %}

elif, else

Evaluates multiple branches

 Hello {% if contact.GENDER == "Male" %} Mr. {{ contact.LASTNAME }}, 
{% elif contact.GENDER == "Female" %} Ms. {{ contact.LASTNAME }}, 
{% else %} there, {% endif %} 

--- 

{% if event.paid %}
   Your spot is reserved. Thanks for submitting your payment.
{% elif event.registered %}
   Your spot is reserved but will be released without payment by January 2.
{% else %}
   There’s still time to sign up! Click here.
{% endif %}

Understanding how if statements are processed

💡 Good to know
If you find this section too technical, we recommend you skip ahead to the Practical examples section of this article.

The if condition is what goes inside the {% if ... %} and {% elif ... %} tags. When processing your if statement, the system will evaluate the condition and:

  • if the condition is met, the following line will be displayed, assuming it is a line of text (it could also be a line of code as you can use our templating language to carry out an operation from within the if statement, in which case the line of code will be run).
  • if the condition is not met, the following line will be ignored and the system will move on to evaluate the next {% elif ... %} condition (if there is one), or process the catch-all {% else %} clause (again, if there is one). Otherwise, it will reach the {% endif %} tag and nothing else will happen as part of this if statement.

As soon as one condition has been met, the system will skip any remaining {% elif ... %} conditions or catch-all {% else %} clause. This means that sometimes you need to think about the best order in which to arrange the conditions within the if statement (other times, the order will not matter).

Building your own if conditions

To match a piece of text (also known as a “string”) in the case of a “Text” attribute, you could use: {% if contact.GENDER == "Male" %}. This condition would cause the following line in the statement to be processed only if the attribute contact.GENDER is equal to “Male”.

If you would like to evaluate whether an attribute contains a value, only the attribute name needs to be placed in the condition, with no further logic. For example, for {% if contact.FIRSTNAME %}, if there is data stored in contact.FIRSTNAME, the following line in the statement will be processed, whereas if contact.FIRSTNAME is empty, the following line will be skipped.

This can also be used in the case of “Boolean” attributes. The condition {% if contact.IS_REGISTERED %} would be fulfilled if contact.IS_REGISTERED is a "Boolean" attribute set to “true”, whereas if it were set to “false” or contained no value, the condition would not be met.

Practical examples

Here are some practical examples based on the most commonly used if statements.

Use an if statement to display supplementary text for certain contacts only

Say for example your contact list includes a “Boolean” attribute called "MEMBER" which is set to "true" if the contact has purchased a membership. You might want to add a supplementary message for those contacts. You could achieve this with the following if statement:  

{% if contact.MEMBER %}
Thanks for renewing your membership!
{% endif %}

Display a personalized greeting when the contact’s name is know, and a generic greeting otherwise

This is useful if you would like to include a greeting like "Dear {{ contact.FIRSTNAME }}" in your email, but not all of your contacts have data for the FIRSTNAME attribute.

{% if contact.FIRSTNAME %}
Dear {{ contact.FIRSTNAME }},
{% else %}
Dear client,
{% endif %}

Display a civility according to your contact’s gender, and a generic greeting otherwise

In addition to using if conditions to check for true values or values that simply exist, you can also check for false values, or evaluate multiple conditions or multiple branches. 

Following the common example of personalizing an email greeting, you may wish to display one of three possible greetings depending on whether the contact's attribute for GENDER is Male, Female, or empty. In this case, you can use the tags {% elif %} to add alternate content when the value for GENDER is Female, and {% else %} to add a back-up for any other case:

Hello {% if contact.GENDER == "Male" %} Mr. {{ contact.LASTNAME }}, 
{% elif contact.GENDER == "Female" %} Ms. {{ contact.LASTNAME }},
{% else %} there, {% endif %}

⏩ What's next?

While these examples focused on structuring if conditions with contact attributes, you may also apply these conditions to transactional parameters within the Drag & Drop Editor (Classic) or by manually building them in your email: