How to write Twig for smart emails

Email templates

Whenever Zenario sends an email, a template is used. Templates may be any kind of automated message which may contain text, links, images or user details. Some examples are form submission confirmations, password reset messages, consent requests or admin notifications.

Templates can be managed in Organizer, in the Email section.

Organizer email templates

Email templates can be edited using rich text or HTML and Twig code.

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 Twig in email templates: example

Starting with version 8.4, Zenario allows the use of Twig snippets in email templates. Text may be formatted using HTML, and merge fields may be used to retrieve data. This is a sample template written using Twig:

Email template merge fields with select list

The message will look like this:

Twig email sample



Merge fields

Every form input field has a merge name, as well as an optional custom code name (e.g. "message" instead of the default "unlinked_textarea_1"). These can be found on the left-hand side panel when editing a form:

Form field merge name

To use a merge field in an email template, put double braces around the code name {{like this}}. Starting with version 8.4, the default merge name and a custom code name (if assigned) can be used interchangeably.

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

Email template merge fields


Twig statements

To execute a Twig statement, put it inside a delimiter: {% ... %}. One example of supported statements is an "if" statement.

If/Elseif/Else statements and conditional operators

Twig framework supports if / elseif / else statements. The syntax is described below.

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.



The template below is written so that the user will be greeted:

- using their full name if both the first and last name were entered,

- or using their first name only if the last name was not entered.

Email template Twig full name or email



First name only:

Twig email template sample first name only


First and last name:

Twig email template sample first and last name


Escaping HTML

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

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.


To find out more about Twig, visit the project homepage.