Twig tags are control tools that direct the flow of the templates. Tags are a mix of blocks and conditionals.
if and elseif
if
statement checks if something evaluates to true. If it does, the block content up to the (mandatory){% endif %}
is processed. This can be a part of the HTML code.In this example below, we are looking for system field #3, which is the email address. If the contact has an example.com email address, it is displayed so. Otherwise it is stated that it has a different address.
{% if "@example.com" in contact.3 %}
this is an example.com email address
{% else %}
this is not an example.com email address
{% endif %}
if
statement you can also define an empty field{% '' %}
. See example below:{% if contact.1 !='' %}Dear {{contact.1}}
{% else %}Hallo!{% endif %}
if
example to define if today is someone’ birthday:
{% if contact.4|localized_date('en','Mdd') == 'now'|localized_date('en','Mdd') %}
Happy birthday !
{% endif %}
In the next example we are targeting our customers based on their gender. We can show different products to them, based on the above query.
{% if contact.5 == 1 %}
this is for him
{% else %}
this is for her
{% endif
if
statement with therequired
filter, the filter will be evaluated before the if statement.if
statement, the only mandatory pieces are{% if foo == 'bar' %}
and{% endif %}
.else
andelseif
are useful, but optional only.elseif
is used exclusively for checking another condition after the previous one has failed. If you do not have a second condition to check, you should be usingelse
.{% elseif %}
is invalid. It should never be used without a precedingif
and the precedingif
must not be already closed withendif
.
Usingelseif
for our example where we are targeting our customers based on their gender. We can show different products to them.
{% if contact.5 == 1 %}
this is for him
{% elseif contact.5 == 2 %}
this is for her
{% endif %}
In another example you can use ESL variables withelseif
to define someone's zodiac sign for their horoscope.
Hey {% if contact.4|localized_date('en','Mdd') > 1221 or contact.4|localized_date('en','Mdd') < 120 %}
Capricorn
{% elseif contact.4|localized_date('en','Mdd') > 1122 %}
Sagittarius
{% elseif contact.4|localized_date('en','Mdd') > 1022 %}
Scorpio
{% elseif contact.4|localized_date('en','Mdd') > 922 %}
Libra
{% elseif contact.4|localized_date('en','Mdd') > 822 %}
Virgo
{% elseif contact.4|localized_date('en','Mdd') > 722 %}
Leo
{% elseif contact.4|localized_date('en','Mdd') > 620 %}
Cancer
{% elseif contact.4|localized_date('en','Mdd') > 520 %}
Gemini
{% elseif contact.4|localized_date('en','Mdd') > 419 %}
Taurus
{% elseif contact.4|localized_date('en','Mdd') > 320 %}
Aries
{% elseif contact.4|localized_date('en','Mdd') > 218 %}
Pisces
{% elseif contact.4|localized_date('en','Mdd') > 120 %}
Aquarius
{% endif %}, it's time to celebrate
foreach
Theforeach
tag works only on arrays, and is used to loop through each key-value pair in an array.
This example loops through the numbers from 1 to 4, and stops at 3 as the limit is set to the 3rd element. The default and maximum limit is 100.
{% foreach asd in [1, 2, 3, 4] limit 3 %}
{{ asd }}
{% endforeach %}