Troubleshooting TWIG_BLOCK in Mautic Email Templates

I came across an issue that I believe many Mautic users might face when combining Twig and MJML for email templates, especially when trying to use RSS feeds. Here’s my experience and how I managed to resolve it:

I was developing an email template with RSS functionality, using this great plugin: Mautic Advanced Templates Bundle. The plugin allows you to use {% TWIG_BLOCK %} in your emails, making it possible to dynamically generate content like RSS feeds. It also ensures Mautic tracks user interactions, such as clicks on RSS article CTAs or links.

The problem arose when I attempted to use TWIG_BLOCK in an MJML-based email template. Mautic did not recognize the Twig block, leading to an error in the logs:

[2024-12-04T11:34:40.890026+00:00] mautic.CRITICAL: Uncaught PHP Exception Twig\Error\SyntaxError: "Unknown "TWIG_BLOCK" tag." at /home/ferrehco/[MAUTIC INSTANCE URL]/themes/vyh-rss-2/html/email.mjml.twig line 38 {"exception":"[object] (Twig\\Error\\SyntaxError(code: 0): Unknown \"TWIG_BLOCK\" tag. at /home/ferrehco/[MAUTIC INSTANCE URL]/themes/vyh-rss-2/html/email.mjml.twig:38)"} {"hostname":"[SERVER]","pid":12926}

The relevant snippet of my email template looked like this:

<mj-column>
  <mj-raw>
    {% TWIG_BLOCK %}
    {% set items = '[WEBSITE RSS]' | rss %}
    {% for item in items | slice(0, 5) %}
      <!-- Render RSS Items -->
    {% endfor %}
    {% END_TWIG_BLOCK %}
  </mj-raw>
</mj-column>

No matter where I placed the Twig block within the email template, Mautic continued to block it. Even using it as a comment, as suggested in another forum post, did not resolve the issue.

After some experimentation, I found a solution that enables the use of TWIG_BLOCK in MJML email templates without triggering errors.

2 Likes

The solution:

Using {% verbatim %} allows Mautic to ignore the {% TWIG_BLOCK %} tag, eliminating the error. This way, you can successfully use the email template for RSS.

<mj-column>
  <mj-raw>
    {% verbatim %}
    {% TWIG_BLOCK %}
    {% set items = '[WEBSITE RSS]' | rss %}
    {% for item in items | slice(0, 5) %}
    <!-- Render RSS Items -->
    {% endfor %}
    {% END_TWIG_BLOCK %}
    {% endverbatim %}
  </mj-raw>
</mj-column>

Before, the error prevented the email template from loading and displayed a blank template instead. Now, your email template will render perfectly!!

1 Like

This might make a nice KB article, just saying :smiley:

1 Like

This topic was automatically closed 36 hours after the last reply. New replies are no longer allowed.