TWIG and MJML

Before i venture down this road,
Has anyone sorted twig logic inside mjml templates and across components without mautic validation kicking in?

Only seen documentation for twig html templates using the advanced templates

Anyone played with this as yet?

End goal is mjml twig templates pulling in dynamic rss feeds and the like

We use it in production.
The problem is: When you add TWIG, the new builder will add

to your code, which is very annoying.
We made the RSS work with some compromises.

What was your work around mate?

Looks like the forum blanked out a section of your response

Currently I am maintaining the template in MJML and then converting it to HTML, then running it through a code template

Error 500 log when I preview the email

[2023-05-04 12:03:06] mautic.CRITICAL: Uncaught PHP Exception Twig\Error\SyntaxError: "Unexpected character "&" in "__string_template__171f72c8e2de59e8c31c4e7214ab2d0a5532d95ceb0c4e3a8f70ff644862fd30" at line 1." at /home/domain/htdocs/domain.com.au/vendor/twig/twig/src/Lexer.php line 363 {"exception":"[object] (Twig\\Error\\SyntaxError(code: 0): Unexpected character \"&\" in \"__string_template__171f72c8e2de59e8c31c4e7214ab2d0a5532d95ceb0c4e3a8f70ff644862fd30\" at line 1. at /home/domain/htdocs/domain.com.au/vendor/twig/twig/src/Lexer.php:363)"} {"hostname":"mautic-cloudpanel-s-1vcpu-1gb-sgp1-01","pid":4170340}

Also if you have any funky methods of nth child logic in Mautic, love to learn

I am hoping to showcase the 1st article (I feel I can do this with twig index),
Then alternate the odds and evens of the index with left/right full mobile fluid (Might be harder)

I’m sorry I should answer before getting my morning coffee… I mixed up the repos.
We use this:

Need to look over where I pulled my version from but I believe this is the same one

Are you using this in HTML or MJML email templates?

All good

Nutted it out.
MJML when saved html encoded theings and converted

  {% TWIG_BLOCK %} {% set items = "https://domain.com.au/feed" | rss %} 

to
{% TWIG_BLOCK %} {% set items = 'https://domain.com.au/feed' | rss %}

nope spoke to soon
MJML on save of email template keeps html encoding within the twig

{% set items = 'https://domain.com.au/feed' | rss %}
or
{% set items = "https://domain.com.au/feed" | rss %}

haha a rather horrific work around but it works

Be nice to fix the source of the issue which is mautic html encoding and validation before mjml conversion.

BUT

  1. Monitor edited records in the emails table in the db
  2. lookup up said record in the bundle_grapesjsbuilder table
  3. run MJML code through the mjml api
  4. update refreshed html back into the relevant emails table.

The conversion happens if you reopen to edit.
Once you fix, save and DO NOT TOUCH, it’s okay.

Do you know why they started encoding the mjml templates?
Is it easy to turn off?

I had to change all of my stylesheets on all my email templates when they did that

haha my style sheets now look like this below, a class for a class in a mj-selector because > was getting encoded.

Gave me a world of hurt

      <mj-selector path=".circle > table > tbody > tr > td > a > img ">
        <mj-html-attribute name="class">circle-item-img
        </mj-html-attribute>
      </mj-selector>
<mj-style>
      .circle-item-img {
      border-radius: 50% !important;
      }
</mj-style>

Check if you can play with

<mj-raw>
twig commands here
</mj-raw>

Yeah mj raw still gets html encoded, as does mj style, i feel mj text is the only module that doesn’t

I use raw as the opening and closing of the loop

Might be worthy of a github issue, see if the mjml cleaning/validation can be an optional parameter.

I get regular errors for this, imorting directly from mjml, that grapes builder is buggy.

If you have time, plz make a bugreport.

@joeyk
Mate I nutted it out.

you comment out the twig code and it doesnt encode. lol
Twig ignores the comments and now it saves safely

This does 2 things

  1. Corrects Mautic and its fun validation/cleaning
  2. MJML didnt like me < > operators I need for index logic, this fixes the mjml bracket issue too
    <mj-raw>
<!--   
      {% TWIG_BLOCK %}
      {% set items = 'https://domain.com/feed' | rss %}
      {% for item in items %}
{% if loop.index <= 1 %}
-->
    </mj-raw>
    <mj-section css-class="section" padding="10px" background-color="#ffffff">
      <mj-column width="65%">
        <mj-text font-size="20px" line-height="24px" color="#212121" font-family="'Montserrat', Trebuchet MS, sans-serif" padding="25px 0 0 0" align="center" font-weight="700" letter-spacing="2px" text-transform="uppercase">
          <a target="_blank" href="{{ item.link }}" class="link" style="color: #212121; text-decoration: none;">{{ item.title }}</a>
        </mj-text>
      </mj-column>
    </mj-section>
    <mj-raw>
<!--   
      {% endif %}
      {% endfor %}
      {% END_TWIG_BLOCK %}
-->
    </mj-raw>
1 Like

Wait a momment…

If you comment it out - the TWIG still gets executed???

haha … YES! :slight_smile:

I got some undesired padding as a result of the empty raw section with no character
ill sort that out next but we have a solid solution

O M G .
All the respect figuring this out.

This has seriously commericalised mautic for me now
Very happy camper here

Will you use RSS feature? There are some seriously powerful features there we started to use.
You can use <campaign>segmentalias</campaign> tag to show the content to specific segment members only. It blew my mind.