In this article, we will explain how to personalize your emails with if
conditions.
What are if
conditions?
if
conditions is the language used in our Block visibility features on the Drag & Drop Editor. 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 using the built-in Block visibility feature. - Custom
if
statements can be built and placed directly within the content of your email campaign or template.
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:
- an email greeting, such as Hello Mr. Smith, Hello Mrs. Brown or simply Hello for contacts without gender or name data on file.
- 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.
- a promotional message, such as including an extra marketing offer in your transactional emails based on their purchases or other attributes.
Structure of an if
statement
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 if
statement:
if |
Checks if a value is true or if an array is not empty |
{% if contact.ACTIVE %} --- {% if params.tutors %} {% for tutor in params.tutors %} |
== |
Checks if an expression is true |
{% if coupon == "WELCOME" %} --- {% if contact.DONOR == true %} |
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" %} --- {% if "Piano" in params.types %} |
not |
check for values that are false |
{% if not user.subscribed %} |
and / or |
Evaluates multiple conditions |
{% if temperature > 10 and temperature < 55 %} --- {% if contact.LANG == "FR" and contact.COUNTRY == "Canada" %} --- {% if contact.COUNTRY == "United States" or contact.COUNTRY == "Canada" %} |
elif, else |
Evaluates multiple branches |
Hello {% if contact.GENDER == "Male" %} Mr. {{ contact.LASTNAME }}, --- {% if event.paid %} |
Understanding how if
statements are processed
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 thisif
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 focus on structuring if
conditions with contact attributes, you may also apply these conditions to contact attributes, transactional parameters, and repeatable blocks within the Drag & Drop editor:
- How to customize your transactional emails
- Show or hide blocks based on contact attributes
- Show or hide blocks based on transactional parameters
- Show or hide blocks based on repeatable blocks
🤔 Have a question?
If you have a question, feel free to contact our support team by creating a ticket from your account. If you don't have an account yet, you can contact us here.
If you’re looking for help with a project using Brevo, we can match you with the right certified Brevo expert partner.