Here are useful Twig snippets for personalizing Mautic emails, landing pages, and text messages.

Access Primary Company Details

Use the contact.primaryCompany shortcut to easily access the primary company associated with the contact. If no company is explicitly marked as primary, this will return the first company associated. It returns null if no companies are linked.

{% if contact.primaryCompany %}
    <p>Your Primary Company: {{ contact.primaryCompany.companyname }}</p>
    {# Access any standard or custom company field #}
    <p>Industry: {{ contact.primaryCompany.companyindustry|default('Not specified') }}</p>
    <p>Website: {{ contact.primaryCompany.companywebsite|default('N/A') }}</p>
{% else %}
    <p>You are not associated with any primary company in our records.</p>
{% endif %}

List All Associated Companies

Iterate through all companies linked to the contact using contact.companies. This is useful if a contact might be associated with multiple companies or if you need to display a complete list.

{% 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 %}
                {# Add more company details if needed #}
                - {{ company.companycity|default('City Unknown') }}
            </li>
        {% endfor %}
    </ul>
{% else %}
    <p>No companies associated with this contact.</p>
{% endif %}

Comma-Separated List of Company Names

Display a simple comma-separated list of the names of all companies associated with the contact.

{% if contact.companies is not empty %}
  <p>Your Companies: {{ contact.companies|map(c => c.companyname)|join(', ') }}</p>
{% else %}
  <p>No companies associated.</p>
{% endif %}

Contact’s Tags

Iterate through and display a contact’s tags.

<p>Your tags:</p>
<ul>
{% for tag in contact.tags %}
    <li>{{ tag }}</li>
{% else %}
    {# This part is shown if contact.tags is empty #}
    <li>No tags assigned.</li>
{% endfor %}
</ul>

Contact’s Segments

Iterate through and display the names of segments a contact belongs to.

{% if contact.segments is not empty %}
  <p>You are in these segments:</p>
  <ul>
  {% for segment in contact.segments %}
      <li>{{ segment.name }}</li>
  {% endfor %}
  </ul>
{% else %}
  <p>Not currently in any segments.</p>
{% endif %}

Get Content from External URL

Fetch content from a URL, decode JSON, and process it as an array. Useful for dynamic content like abandoned cart details.

{# Example: Fetching product details from an API endpoint #}
{% set product_url = 'https://api.yourstore.com/products/' ~ contact.last_viewed_product_id %}
{% set product_data = product_url | get_content_from_url | json_decode %}

{% if product_data %}
  <p>Still interested in the {{ product_data.name }}?</p>
  <p>Price: {{ product_data.price }}</p>
  <p><a href="{{ product_data.url }}">View Product</a></p>
{% endif %}

{# Example: Fetching simple text content #}
{% set announcement = 'https://your.site/announcement.txt' | get_content_from_url %}
<p>Latest News: {{ announcement }}</p>

Content Segmentation based on Contact Affinity

Display different content based on a contact field value (e.g., affinity).

{% if contact.affinity == 'private' %}
<p>This is Private Content, tailored for individual users.</p>
{% elseif contact.affinity == 'b2b' %}
<p>This is B2B Content, relevant for business clients.</p>
{% elseif contact.affinity == 'agency' %}
<p>This is Agency Content, specifically for our partners.</p>
{% else %}
<p>Here's some general information.</p>
{% endif %}

Always Capitalize

Ensure text, like a contact’s first name, is capitalized.

{{ contact.firstname|capitalize }}

Fetch RSS Data

Retrieve and display content from an RSS feed.

{% set feed = 'https://your.rss.feed.url' | rss %}

{% if feed and feed.channel %}
  <h1>{{ feed.channel.title }}</h1>
  <p><a href="{{ feed.channel.link }}">{{ feed.channel.link }}</a></p>
  <p>Published: {{ feed.channel.pubDate }}</p>
  <p>{{ feed.channel.description }}</p>

  <h2>Articles</h2>
  <ul>
      {% for item in feed.channel.item %}
          <li>
              <h3><a href="{{ item.link }}">{{ item.title }}</a></h3>
              <p>Published: {{ item.pubDate }}</p>
                   {{ item.description|raw }} {# Use raw filter if description contains HTML #}
          </li>
      {% else %}
          <li>No articles found in the feed.</li>
      {% endfor %}
  </ul>
{% else %}
  <p>Could not load the RSS feed.</p>
{% endif %}

Expiry Date Calculation

Calculate the difference between a date field (e.g., date_last_order) and the current date.

{% set expiry_date_str = contact.date_last_order %} {# Assuming YYYY-MM-DD format #}
{% if expiry_date_str %}
  {% set expiry_date = expiry_date_str|date('U') %} {# Convert to Unix timestamp #}
  {% set today = "now"|date('U') %}
  {% set difference_seconds = expiry_date - today %}
  {% set days_left = (difference_seconds / (60*60*24))|round(0, 'floor') %}

  {% if days_left < 0 %}
    <p>Your offer has expired.</p>
  {% elseif days_left == 0 %}
    <p>Your offer expires today!</p>
  {% elseif days_left == 1 %}
    <p>Your offer expires tomorrow (1 day left).</p>
  {% else %}
    <p>Your offer expires in {{ days_left }} days.</p>
  {% endif %}
{% else %}
  <p>No expiry date found.</p>
{% endif %}

{# Simpler example for days *since* a date #}
{% set last_order_date_str = contact.date_last_order %}
{% if last_order_date_str %}
    {% set last_order_date = date(last_order_date_str) %}
    {% set today = date("now") %}
    {% set difference = today.diff(last_order_date) %}
    {% set days_ago = difference.days %}
    <p>Your last order was {{ days_ago }} days ago.</p>
{% endif %}

Time-Based Greetings

Display greetings based on the current time of day (server time).

{% set currentHour = "now"|date("H") %}
{% if currentHour >= 5 and currentHour < 12 %}
    <p>Good morning, {{ contact.firstname|default('there') }}!</p>
{% elseif currentHour >= 12 and currentHour < 18 %}
    <p>Good afternoon, {{ contact.firstname|default('there') }}!</p>
{% else %}
    <p>Good evening, {{ contact.firstname|default('there') }}!</p>
{% endif %}

Fallback Value

Provide a default value if a contact field is empty.

<p>Your preferred language is {{ contact.preferred_language|default('English') }}.</p>
<p>Welcome, {{ contact.firstname|default('Valued Customer') }}!</p>

Magic with Dates

Manipulate and format dates.

Tomorrow is {{ "now"|date_modify('+1 day')|date("l, F jS, Y") }}

Today's date: {{ "now"|date("Y-m-d") }}

The current month is {{ "now"|date("F") }}

Random Selection

Choose a random item from a list. Useful for varying greetings or calls to action.

{# Random greeting variation #}
<p>{{ random(['Hello', 'Hi', 'Hey there']) }}, {{ contact.firstname|default('friend') }}!</p>

{# Random call to action #}
<a href="/offer">{{ random(['Learn More', 'Get Started', 'Discover Now']) }}</a>

Gender-Based Proper Greeting (German Example)

Customize greetings based on gender using a contact’s title field.

{% if contact.title == "Frau" %}
  <p>Sehr geehrte {{ contact.title }} {{ contact.lastname }},</p>
{% elseif contact.title == "Herr" %}
 <p>Sehr geehrter {{ contact.title }} {{ contact.lastname }},</p>
{% else %}
  {# Fallback for other titles or no title #}
  <p>Guten Tag {{ contact.firstname|default('') }} {{ contact.lastname }},</p>
{% endif %}

Personalize Content with JSON Decode

Process JSON stored in a contact field (e.g., cart_data) to display personalized content like cart items or order details.

{% set cart_json = contact.cart_data %}
{% if cart_json %}
  {% set cart = cart_json | json_decode %}
  {% if cart and cart.items is not empty %}
    <p>Items left in your cart:</p>
    <ul>
    {% for item in cart.items %}
      <li>{{ item.name }} - Quantity: {{ item.quantity }} - Price: {{ item.price }}</li>
    {% endfor %}
    </ul>
    <p>Total: {{ cart.total_price }}</p>
    <p><a href="https://yourstore.com/cart">Complete your purchase</a></p>
  {% else %}
    <p>Your cart is currently empty.</p>
  {% endif %}
{% else %}
  <p>Visit our store to start shopping!</p>
{% endif %}

Support for Email Tokens in API

Use email tokens when sending emails via the API by passing them in the tokens key of the payload. See the API Documentation for more details.

{# Example: Assuming you passed {'{discount_code}': 'SUMMER20'} in the API payload #}
<p>Your special discount code is: {{ tokens['{discount_code}']|default('No code available') }}</p>

{# Example: Using a token for a personalized link #}
{% set tracking_link = tokens['{tracking_url}']|default('https://yoursite.com') %}
<p><a href="{{ tracking_link }}">Track your order</a></p>