The Twig Templates plugin provides several objects and tokens you can use to access Mautic data and perform actions within your templates.

Contact Object (contact)

The contact object provides access to the fields of the contact viewing the email or landing page.

  • Standard Fields: Access any standard Mautic contact field using its alias (e.g., contact.firstname, contact.email, contact.city).
    Hello {{ contact.firstname|default('there') }}!
    Your email is {{ contact.email }}.
    
  • Custom Fields: Access custom contact fields using their alias (e.g., contact.custom_field_alias).
    Your preference: {{ contact.preference_field }}
    
  • Tags: Access the contact’s tags as an array.
    {% if 'VIP' in contact.tags %}
      <p>Welcome, VIP!</p>
    {% endif %}
    
    <ul>
    {% for tag in contact.tags %}
        <li>{{ tag }}</li>
    {% endfor %}
    </ul>
    
  • Segments: Access the segments (lists) the contact belongs to as an array of objects. Each object has properties like id and name.
    {% for segment in contact.segments %}
        {% if segment.name == 'Newsletter Subscribers' %}
            <p>Thanks for subscribing!</p>
        {% endif %}
    {% endfor %}
    
  • Companies (contact.companies): Access all companies associated with the contact as an array of company objects. Each object contains the company’s fields.
    {% if contact.companies is not empty %}
        <p>Associated Companies:</p>
        <ul>
            {% for company in contact.companies %}
                <li>
                    {{ company.companyname }}
                    {% if company.is_primary == '1' %} (Primary){% endif %}
                    - {{ company.companycity|default('City Unknown') }}
                </li>
            {% endfor %}
        </ul>
    {% else %}
        <p>No companies associated with this contact.</p>
    {% endif %}
    
  • Primary Company Shortcut (contact.primaryCompany): Returns the primary company object if one is set. If no company is explicitly marked as primary, it returns the first company associated with the contact. Returns null if the contact has no associated companies.
    {% if contact.primaryCompany %}
        <p>Primary/First Company Name: {{ contact.primaryCompany.companyname }}</p>
        <p>Industry: {{ contact.primaryCompany.companyindustry|default('Not specified') }}</p>
    {% else %}
        <p>No company associated.</p>
    {% endif %}
    

Form Result Tokens

These tokens are available only when an email is triggered by a form submission action (either directly or via a campaign). See Forms Support for details.

  • formresult: An object containing the data from the most recent form submission by the contact. Field names correspond to form field aliases.
    Thank you for submitting the form, {{ formresult.firstname }}!
    Your message: {{ formresult.message_field }}
    
  • formresults: An array containing all form submission data associated with the contact. Each element is an object like formresult.
    <p>Submission History:</p>
    <ul>
    {% for result in formresults %}
        <li>{{ result.dateSubmitted|date('Y-m-d') }}: {{ result.email }}</li>
    {% else %}
        <li>No submissions recorded.</li>
    {% endfor %}
    </ul>
    

Email Tokens (tokens)

When sending emails via the API, you can pass custom tokens in the payload. These are accessible via the tokens object. See the API Documentation for usage.

{# Assuming 'mycustomtoken' was passed in the API payload #}
Your special code is: {{ tokens['{mycustomtoken}'] }}

This list covers tokens specifically added or enhanced by the Twig Templates plugin. Standard Twig variables and functions (like now for the current time) are also available. Refer to the official Twig Documentation for more on standard Twig capabilities. For accessing Custom Objects data, see the Custom Objects Support page.