Using Twig Templates
Guide on how to effectively use Twig templates for Mautic personalization.
Learn how to leverage Twig templates for customizing your Mautic experience. Twig provides a powerful yet simple syntax for adding logic directly into your Mautic assets.
Basic Example: Personalized Greeting
Here’s a fundamental example showing how to greet a contact by their first name, with a fallback if the name isn’t available:
This example demonstrates:
- Using an
if/else
block to check ifcontact.firstname
exists. - Using the
default
filter as a shorthand to provide a fallback value directly within the output{{ }}
tags.
Learning Twig
The Twig syntax is designed to be easy to learn. You don’t need to stress over complex syntax.
For more examples and ready-to-use snippets, check out our Twig Templates Snippets Library.
Reusing Twig Templates
You can insert one saved Twig Template within another using a special token format. This allows you to create reusable pieces of content or logic (like a standard footer or a complex offer block) and use them in multiple emails or pages without copying and pasting the code.
Use the {twigtemplate=TEMPLATE_NAME}
token, replacing TEMPLATE_NAME
with the exact name of the Twig Template you created and saved in Mautic.
Example: Tier-Based Promotional Offers
Imagine you want to show different offers to contacts based on their membership level, stored in a custom field called membership_tier
. You could create separate Twig Templates for each offer (e.g., save templates named Gold Member Offer
, Silver Member Offer
, Standard Welcome Offer
).
Then, in your main email or landing page template, you can use conditional logic to insert the correct offer template based on the contact’s membership_tier
field:
This approach makes your main template much tidier and easier to manage. If you need to update an offer, you only edit the specific saved template (e.g., Gold Member Offer
), and the change automatically applies everywhere it’s inserted. Just ensure the template names you reference exactly match those saved in your Mautic Twig Templates list.
Using Dynamic Template Names
You can use Twig tokens inside the template name within the {twigtemplate=...}
token to dynamically choose which template to insert. This simplifies your main template when dealing with variations based on contact data.
Example: Simplified Tier-Based Offers
Following the previous example, if you named your saved templates consistently based on the membership tier (e.g., offer-gold
, offer-silver
, offer-standard
), you could replace the if/elseif/else
block with a single dynamic token:
This achieves the same result as the conditional example, just more concisely. Mautic first processes the Twig inside the token (e.g., {{ contact.membership_tier|default('standard') }}
) to get the final template name (like offer-gold
). Then, it inserts the content of the matching saved Twig Template.
Ensure the generated names exactly match your saved Twig Template names.