An if
statement checks if an expression evaluates to true.
If it does, the block content is processed up to the mandatory line {% endif %}
(with which you close the statement).
You can include the line {% else %}
if you want to specify what should be displayed if the first part of the statement didn’t evaluate to true.
This can be a part of the HTML code as well.
In the example below, we are looking for Contact field #3 in the SAP Emarsys Contact database, which is the email address. If the contact has an example.com email address, it will be displayed.
The code snippet looks like as follows:
{% if "@example.com" in contact.3 %}
@example.com
{% endif %}
If the contact’s email address doesn’t contain @example.com, then nothing will be displayed.
Let’s see how to use this in the VCE Editor.
And this is how it will render to a contact who evaluates to true for the expression.
If the contact doesn’t evaluate to true, then this will be the rendered email.
If you would like to display different content to those contacts who don’t qualify for the if statement, then add the line {% else %}
to the code snippet.
{% if "@example.com" in contact.3 %}
@example.com
{% else %}
external
{% endif %}
This is how the code snippet looks like in the VCE editor:
This is how the email will be rendered for contacts who do not evaluate to true:
In an if
statement you can compare variables with other variables or constants like numbers or pieces of text (a piece of text has to be enclosed by apostrophes like this {% ' ' %}
. For example:
{% if contact.1 !='' %}Dear {{contact.1}}
{% else %}Hello!{% endif %}
In the following example, if there is a value in the Contact.1
data field, then we will display Dear + First name.
If the Contact.1
field is empty, then we’ll display Hello!
!=
in this example means that the expression will be true if the right side and the left side are not equal. So if there is a value in the Contact.1
field (i.e. it’s not empty) then Dear <First_Name> will be displayed.
For those contacts, whose Contact.1
field is not empty, the email will look like as follows:
If the contact’s Contact.1
field is empty, then this will be their email:
In the following example, we will use an if
statement that displays Happy Birthday! if the contact’s birthday is on the given day.
In the Emarsys Contact database Contact field #4 contains the contact’s birth date that we use in this if
statement.
We are use the localized_date
filter to make sure that both the birth date and the current time are formatted the same way. To read more about localized_date
filter, click here and here.
The code snippet looks like this.
{% if contact.4|localized_date('en','Mdd') == 'now'|localized_date('en','Mdd') %}
Happy birthday !
{% endif %}
And the rendered email will look like this:
In this example, we will show you how to display different content to your contacts based on their gender. Data field #5 contains the gender information of each contact in the Emarsys Contact database, so we will use it in this if
statement.
The code snippet looks like this.
{% if contact.5 == 1 %}
Check out our vast selection of jackets and ties.
{% else %}
Check out our vast selection of skirts and shorts.
{% endif %}
{{ Contact.5 }}
is a single-choice data field, meaning that contacts can select only one option from multiple ones. In this case, contacts can choose from the following options: female, male, prefer not to say. A single-choice data field’s value has a Choice ID (1, 2, 3) and the actual Choice (male, female, prefer not to say). In the example above, the line {% if contact.5 == 1 %}
means that specific content will be displayed for the contacts if their gender equals 1 (i.e. male).
In VCE this is how the template will look like:
This is what contacts will see if their gender is set to female:
And this is what contacts will see if their gender is set to male: