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:

{# Check if the contact's firstname field has a value #}
{% if contact.firstname %}
  <p>Hello {{ contact.firstname }}!</p>
{% else %}
  <p>Hello there!</p>
{% endif %}

{# A more concise way using the 'default' filter #}
<p>Welcome, {{ contact.firstname|default('Valued Customer') }}!</p>

This example demonstrates:

  1. Using an if/else block to check if contact.firstname exists.
  2. 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:

<h2>Your Special Offer</h2>

{% if contact.membership_tier == 'gold' %}
  {twigtemplate=Gold Member Offer}
{% elseif contact.membership_tier == 'silver' %}
  {twigtemplate=Silver Member Offer}
{% else %}
  {# Default offer for standard members or if tier is not set #}
  {twigtemplate=Standard Welcome Offer}
{% endif %}

<p>Thank you for being a customer!</p>

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:

<h2>Your Special Offer</h2>

{# Dynamically inserts 'offer-gold', 'offer-silver', or 'offer-standard' #}
{twigtemplate=offer-{{ contact.membership_tier|default('standard') }}}

<p>Thank you for being a customer!</p>

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.