Twig Templates Forms Support
Use data from Mautic form submissions in your Twig templates.
You can use data that contacts submit through Mautic forms directly in your emails or landing pages using Twig Templates.
Form Data Tokens
When using Twig Templates, you can use these special tokens to access form submission data:
-
{{ formresult }}
: Contains the data from the single, most recent form submission by the contact for the form that triggered the action. -
{{ formresults }}
: Contains a list (array) of all form submissions made by the contact for the triggering form. -
getFormResults(formId, contactId, limit, page, orderBy, orderDirection)
:
A Twig function to fetch form submissions 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 ornull
, 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 ornull
, 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):
- Example (Show the total number of submissions for Form 12 across all contacts):
These tokens can be used in emails, landing pages, and other places where Twig Templates are supported.
Fetching Results Count for Any Form (getFormResultsCount
)
You can use the getFormResultsCount
function to set a registration cap for a form. For example, if you want to stop accepting new registrations after a certain number of people have registered, you can use Twig logic to display a message instead of the form.
Example: Limit Registrations and Show “Registration Complete” Message
This approach works in landing pages and custom form templates where you can use Twig logic. The form will only be shown if the registration limit has not been reached.
Fetching Results for Any Form (getFormResults
)
You can use the getFormResults
Twig function to fetch submissions for any form by its ID, with optional filters and pagination.
Each submission
in the results contains all the form field values, accessible by their alias (the unique name you gave the field). For example, if your form has fields with aliases firstname
, email
, and company
, you can access them as submission.firstname
, submission.email
, and submission.company
.
You also get some built-in properties like submission.dateSubmitted
and submission.leadId
.
Example: Display Recent Form Submissions in a Table
Let’s say you want to show the last 3 times a contact filled out Form 12. Here’s how:
This will show:
- A table with the submission date, name, email and company
- Only the last 3 submissions
- A friendly message if no submissions exist
Example: Show Recent Form Submissions from All Contacts
Want to see who’s been submitting Form 12? Here’s how to list the last 5 submissions:
This will display:
- A list of emails from the last 5 submissions
- The date they submitted
- A friendly message if no one has submitted yet
This lets you show real-world data, like a contact’s submission history, or display recent leads from a form, directly in your emails or landing pages.
Using the Last Submission (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.
To show data from the contact’s latest form submission, use the formresult
token. You access the submitted values using the form field’s alias (the unique name you gave the field).
This creates a more polished thank you message that:
- Greets the user by name
- Clearly lists what they submitted
- Uses labels and values for better readability
- Ends with a friendly closing
Using All Submissions (formresults
)
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.
If you need to show data from older submissions, you can loop through the formresults
list. Each item in the list (result
in the example below) works like the formresult
token.