sighmon / mjml-rails

MJML + ERb/Haml/Slim view template

Home Page:https://mjml.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem processing non-ERB (Haml, Slim) layouts

aleksandrs-ledovskis opened this issue · comments

In #33 / v4.2.0 a change in logic was made around assumption that layout template will always contain verbatim <mjml>.

In ERb world this (mostly) holds, but when layout is composed using advanced templating languages like Haml and Slim, both allowing tag definition without <> wrapping, it fails.

Also, it doesn't handle edge case where somebody is referencing/writing code comment in partial that is e.g. <%# Let's use MJML in this partial. Note that <mjml> must reside in layout %> - currently, mjml-rails will take that comment's <mjml> as flag to process template with MJML library. Or when root tag is generated using content_tag. Or when it's contained in a partial.

Current workaround: Put verbatim <mjml> in Haml/Slim layouts. Avoid dynamically generating MJML root tag. Don't reference <mjml> proper anywhere else except MJML layout files.


Simple change of

if template.source =~ /<mjml>/

to

if compiled_source =~ /<mjml>/

would solve no <> and comment-triggering-MJML situations, but still leave other aforementioned edge cases broken.


Possible solutions:

  1. Doing first-pass render of template to process nested partials/helpers, capture this output and use same match (=~ /<mjml>/) as done currently.

  2. Require some kind of magic comment in file (e.g. # mjml_processing: true) that would serve as signal to do MJML processing of rendered file.

  3. Require users to whitelist paths to MJML-processed layout/legacy-template files in initializer, akin to Asset Pipeline compilation does. Then just match against template.identifer


/cc @sighmon

@aleksandrs-ledovskis I haven't used Haml or Slim, so will take your recommendation for the best way forward.

  1. sounds like a pretty decent way forward to me if it's similar to how Asset Pipeline compilation handles things.

But again, please use your best judgement. :-)

commented

For the time being the documentation should contain a hint that other template languages than ERB are currently broken.

@biastoch2 @jbwl would your favoured solutions be to change if template.source =~ /<mjml>/ to if compiled_source =~ /<mjml>/ as you've done in your fork @biatoch2 ?

commented

I tried the fix from @biatoch2 and it works for me. I'd vote for it.