Basics of Working with Templates
Templates support specific variables and a special syntax that provides more possibilities than static templates. In other words, certain text is transformed according to predefined rules. If you have worked with the Twig templating engine, then you already know almost everything you need. This page describes the basic available functionality. Those who want to learn more in detail can refer to the official documentation for Twig. However, not all Twig features are available in our templates, so be sure to check the result using the preview function and also send test campaigns to your own accounts before launching a campaign to a large number of real users.
Available variables in templates:
- subscriber — an object that contains information about the current subscriber. You can view the full structure of this variable on a separate page.
- campaign — an object that contains information about the current email campaign. You can view the full structure of this variable on a separate page. This variable will not contain any data in preview mode, as it works outside the context of any email campaign.
- unsubscribe_url — an HTTP link to our page where the subscriber can unsubscribe from the current email campaign. If you have your own subscription settings on your website, you can use an unsubscribe link that leads to your site. If a subscriber cannot find a simple and convenient way to unsubscribe from unwanted emails, they will likely mark your message as spam, so we recommend providing an unsubscribe option in every email. This variable will not be available in preview mode.
Variables Can Be Used in Templates
To use a variable, it must be enclosed in double curly braces. For example, to output the variable test126, you need to write {{ test126 }}. Case sensitivity (uppercase and lowercase letters) is mandatory, while spaces are optional. That is, you can write {{test126}} or {{ test126 }}, but {{ TESt126 }} is not allowed. The variable test126 is just an example; such a variable does not actually exist.
If a variable is a complex object with its own properties, you can access them using a dot. For example, accessing the id property of the subscriber variable looks like this: {{ subscriber.id }}. This is a real example — the subscriber variable is available in templates and has an id property. You can see the full list of properties on a separate page. Arrays can be accessed by index using square brackets, but you must be sure that the index exists. For example, {{ subscriber.tags[0] }}. If the subscriber has no tags, attempting to output the first tag (with index 0) will output nothing (that is, an “empty string,” as programmers say).
Basic Operations Can Be Performed on Data
Mathematical operations are supported:
- addition
{{ price + tax }} - multiplication
{{ count * 2 }} - division
{{ total / 3 }}
Conditional Operator (IF)
- basic if
{% if subscriber.is_active %} You are subscribed to the mailing list {% endif %} - if operator with else block
{% if subscriber.is_active %} You are subscribed to the mailing list {% else %} You are unsubscribed from the mailing list {% endif %} - Chain of conditions
{% if subscriber.gender == 'male' %} You are a man {% elseif subscriber.gender == 'female' %} You are a woman {% elseif subscriber.gender == '' %} Your gender is not specified {% endif %}
The if operator must have an opening tag {% if condition %} and a closing tag {% endif %}. Inside, you can place any number of {% elseif condition %} sections. Optionally, you can also include a {% else %} section, which will execute without additional conditions if execution reaches it. That is, if the conditions of all previously defined parts of the if operator are not met.
Loops (FOR)
Loops are used to output lists. Let’s assume we have a list stored in the items variable. Here is how it can be iterated:
{% for item in items %} {{ item }}{% endfor %}
The value of the item variable will be different on each iteration of the loop. Inside the loop, some other variables related to the current iteration are also available:
- loop.index — iteration number, starting from 1
- loop.first — is this the first iteration? (true/false)
- loop.last — is this the last iteration? (true/false)
A loop can be combined with a check for the presence of data:
{% for item in items %} {{ item }}{% else %} The items array is empty{% endfor %}