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

You can access form submission data in your Twig templates using special variables and a new function:

  • formresult: An object containing the data from the most recent form submission by the contact for the form that triggered the current action. Field names correspond to form field aliases.

    Important: This only works if the email is sent using:
    • A “Send email to contact” action directly on a form.
    • A “Send email” action in a campaign that starts when a form is submitted.

    Thank you for submitting the form, {{ formresult.firstname }}!
    Your message: {{ formresult.message_field }}
    
  • formresults: An array containing all form submission data for the triggering form and contact. Each element is an object like formresult.

    Important: This only works if the email is sent using:
    • A “Send email to contact” action directly on a form.
    • A “Send email” action in a campaign that starts when a form is submitted.

    <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>
    
  • getFormResults(formId, contactId, limit, page, orderBy, orderDirection):
    A Twig function to fetch form submission results for any form by its ID, with optional filters and pagination.

    • formId (required): The ID of the form.
    • contactId (optional): The contact’s ID. If omitted or null, fetches results for all contacts.
    • limit (optional): Max number of results per page.
    • page (optional): Page number (default: 1).
    • orderBy (optional): Field to sort by (default: 's.date_submitted').
    • orderDirection (optional): 'ASC' or 'DESC' (default: 'DESC').
  • getFormResultsCount(formId, contactId):
    A Twig function that fetches the total count of form submissions for a specific form ID.

    • formId: (Required) The integer ID of the Mautic Form you want to query.
    • contactId: (Optional) The integer ID of the Mautic Contact. If provided, counts submissions for that specific contact only. If omitted or null, counts submissions for the form across all contacts.
    • Returns an integer representing the total number of matching submissions.
    • Available in Emails, SMS, and Landing Pages.
    • Example (Show how many times the current contact submitted Form 12):
      {% if contact.id is defined %}
          {% set submissionCount = getFormResultsCount(12, contact.id) %}
          <p>You have submitted Form 12 {{ submissionCount }} time(s).</p>
      {% endif %}
      
    • Example (Show the total number of submissions for Form 12 across all contacts):
      {% set totalSubmissions = getFormResultsCount(12) %}
      <p>Form 12 has been submitted a total of {{ totalSubmissions }} times by all contacts.</p>
      

    Examples:

    {# Get the latest 5 submissions for Form 12 for the current contact #}
    {% if contact.id is defined %}
      {% set results = getFormResults(12, contact.id, 5) %}
      {% for submission in results %}
        <li>{{ submission.dateSubmitted }}: {{ submission.email }}</li>
      {% endfor %}
    {% endif %}
    
    {# Get the latest 3 submissions for Form 12 across all contacts #}
    {% set allResults = getFormResults(12, null, 3) %}
    {% for submission in allResults %}
      <li>{{ submission.dateSubmitted }} by Contact ID {{ submission.leadId }}</li>
    {% endfor %}
    

See Forms Support for more details and examples.

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.