How to write Twig for an email template

What is Twig and why use it?

Twig is a powerful HTML template engine. It offers all the features already available for the rich text editor, such as merge fields, along with other extras (e.g. conditional logic, loops, escaping strings). Zenario core functions can be used in a Twig template.

Using merge fields

Starting with version 8.4, Zenario allows the use of Twig snippets in email templates.

To use a merge field, put double braces around it {{like this}}.

Email template merge fields with select list

A dropdown of all possible merge fields is available above the "Email body" text area.

Twig statements

To execute a Twig statement, put it inside a delimiter: {% ... %}

Escaping HTML

If a string contains characters that might affect the resulting HTML (e.g. an apostrophe), it can be escaped to render these characters harmless by piping the string through the escape filter |e.

Twig escaping a string and javascript

By default, only HTML is escaped, but certain parameters may be used to escape, for example, Javascript ('js'), CSS ('css') or a URL ('url') if needed.

If/Elseif/Else statements and conditional operators

Twig framework supports if / elseif / else statements.

Twig if elseif else statements

Any number of other conditions may be added using {% elseif %} and {% else %} statements. These statements are optional, but the final {% endif %} statement is mandatory.

Operators such as andor, not, >, <, >=, <=, ==, != may be used for more control.

Examples

Email template Twig full name or email

 

The user's full name will be used if both the first and last name were entered:

Email template Twig full name

 

If only one (or neither) was entered, the email address will be sent.

Email template Twig email only