This article explains how to use conditional logic with Twig to dynamically personalize your landing pages, emails, and tickets.
WHAT IS CONDITIONAL LOGIC?
Conditional logic makes decisions based on certain conditions. We all use conditional logic in our daily lives, for example:
- If it is cold outside → wear a coat
- If the guest is a VIP → the coat costs €5
To implement such a condition in a template (email or PDF ticket), use the Twig syntax with curly braces and percent signs:
|
{% if condition %} ... your content ... {% endif %} |
The opening tag contains if followed by the condition, the closing tag endif ends the block. The weather example looks like this:
|
{% if guest.extendedfields.weather == 'cold' %} Wear a coat. {% endif %} |
VARIABLES AND VALUES
In the example 'If the guest is a VIP', VIP is the variable name. The actual variable could be, for example, contact.extended_fields.vip_level. The key is the stored value. This can be yes / no, true / false, or 1 / 0.
The complete example:
|
{% if contact.extended_fields.vip_level == 'yes' %} The coat costs €5. {% endif %} |
COMPARISON OPERATORS
Comparison operators check values against each other. Here is the complete overview:
| Operator | Symbol | Description |
| Equal | == | True if the values are equal |
| Not equal | != | True if the values are not equal |
| Less than | < | True if the left is less than the right |
| Greater than | > | True if the left is greater than the right |
| Less than or equal | <= | True if the left is less than or equal to the right |
| Greater than or equal | >= | True if the left is greater than or equal to the right |
Examples in Email Templates
|
{% if contact.sex == 'female' %} Ms.{{ contact.last_name }}{% endif %} {% if contact.language == 'EN' %} English content here {% endif %} |
Checking Empty Values
Two single quotes '' or double quotes "" represent an empty value. Alternatively, you can use is not empty :
| {% if contact.title != '' %}{{ contact.title }} {% endif %} |
| {% if contact.title is not empty %}{{ contact.title }} {% endif %} |
| Note: Both forms are equivalent. Use the variant that is more readable for you. |
LOGICAL OPERATORS
Logical operators combine multiple conditions and return a single boolean output (true or false). The two operators are and and or.
| Operator | Description |
| and | Returns true if both sides are true |
| or | Returns true if at least one side is true |
Example: and Operator
| Name | Accommodation | Shuttle |
| Devon | yes | yes |
| Nolan | no | no |
| Misha | no | yes |
|
{% if contact.extended_fields.accommodation == 'yes' and contact.extended_fields.shuttle == 'yes' %} Show specific content {% endif %} |
| NOTE: Only Devon sees the content, as only she has both values set to "yes". |
Example: or Operator
| Name | Position |
| Devon | CEO |
| Nolan | CFO |
| Misha | (empty) |
|
{% if contact.extended_fields.position == 'CEO' or contact.extended_fields.position == 'CFO' %} Show specific content {% endif %} |
| NOTE: Devon and Nolan see the content. Misha does not, as the value is empty. |
ADVANCED CONDITIONS: ELSE AND ELSEIF
So far, you have only seen "Do something if the condition is met." With else and elseif you can also define alternatives.
if / else
| {% if contact.sex == 'female' %}Mrs. {% else %}Mr.{% endif %} |
| Attention: If contact.sex is empty, "Mr." will still be displayed because else catches all other cases. |
if / elseif
| {% if contact.sex == 'female' %}Mrs.{% elseif contact.sex == 'male' %}Mr.{% endif %} |
| Note: If the value is neither "female" nor "male", nothing will be displayed. |
if / elseif / else (recommended)
The best solution combines all three variants and covers every case:
| {% if contact.sex == 'female' %}Dear Ms. {% elseif contact.sex == 'male' %}Dear Mr. {% else %}Dear Ms./Mr. {% endif %}{{ contact.last_name }}, |
TEMPLATES TO COPY
You can directly use the following templates.
Personalized salutations on landing pages or in email templates:
Dear (female/male/generic) (DE)
| {% if contact.sex == 'female' %}Liebe {{contact.first_name }}{% elseif contact.sex == 'male' %}Lieber {{contact.first_name}}{% else %}Liebe(r) {{contact.first_name}}{% endif %}, |
Dear Ms / Dear Mr / Dear (first name last name) (DE)
| {% if contact.sex == 'female' %}Liebe Frau {{contact.last_name}}{% elseif contact.sex == 'male' %}Lieber Herr {{contact.last_name}}{% else %}Liebe(r) {{contact.first_name}} {{contact.last_name}}{% endif %}, |
Dear Ms./Mr. with title (DE)
| {% if contact.sex == 'female' %}Sehr geehrte Frau {% if contact.title != '' %}{{contact.title}}{% endif %} {{contact.last_name}}{% elseif contact.sex == 'male' %}Sehr geehrter Herr {% if contact.title != '' %}{{ contact.title}} {% endif %}{{contact.last_name}}{% else %}Sehr geehrte(r) {% if contact.title != '' %} {{contact.title}} {% endif %}{{contact.full_name}}{% endif %}, |
Personalised salutations on landing pages or in email templates:
Dear first name (EN)
| Dear {{contact.first_name}}, |
Dear Ms / Mr last name (EN)
| Dear {% if contact.sex == 'female' %}Ms {% elseif contact.sex == 'male' %}Mr {% else %}Ms/Mr {% endif %}{{contact.last_name}}, |
Dear title last name (EN)
| Dear {% if contact.title != '' %}{{ contact.title }} {% else %}{% if contact.sex == 'female' %}Ms {% elseif contact.sex == 'male' %}Mr {% else %}Ms/Mr {% endif %}{% endif %}{{ contact.last_name }}, |
| Tip: Test your templates with different contact data to ensure all cases (female, male, diverse) are correctly covered. |
If you have any questions, our Customer Service Team is always available to assist you.