sighmon / mjml-rails

MJML + ERb/Haml/Slim view template
https://mjml.io
Other
295 stars 64 forks source link

Support variables as used in Mailjet #85

Closed boardfish closed 2 years ago

boardfish commented 3 years ago

Mailjet's MJML templates allow templating of variables using this syntax: {{var:variable_name}}. data is also used as a key for things that come from the recipient's profile in Mailjet. It'd be good if this library supported templating in that format so that templates are portable between a local environment and Mailjet.

sighmon commented 3 years ago

@boardfish What happens if you wrap them in an <mj-raw> tag?

boardfish commented 3 years ago

If I understood you correctly, using <mj-raw>{{var:name}}</mj-raw> might change things. Unfortunately, there was no change in output. Thanks for the quick response!

sighmon commented 3 years ago

@boardfish And if you wrap them: <mj-text>{{var:name}}</mj-text> does that work any better?

Perhaps I'm missing the problem... you might have to describe it a little clearer for me sorry - it's been a while since I've been back into this project.

boardfish commented 3 years ago

I'll give that a try tomorrow.

The problem I'm facing is that I want to use Mailjet's templating syntax (e.g. {{var:name}}) to inject data into the rendered MJML. I'm aware this gem has support for ERB templating syntax (<%= @name %>) inside MJML, but native support for Mailjet's syntax would be really helpful for my use case. Ideally, {{var:name}} would do the same as <%= @name %> and add the value of @name to the output buffer .

It seems Mailjet's syntax for MJML also includes conditionals ({% if something === "true" %}) among other things, and it's kind of adjacent to Handlebars/Mustache templates.

I might have an alternative solution to my base problem, which is to deal with Mailjet templates locally and render them via Action Mailer. I'll investigate that side of things further - hopefully it renders this issue moot.

boardfish commented 2 years ago

Closing this issue because my alternate solution — passing Mailjet's HTMLContent through a gnarly regex and processing it with Mustache — appears to have worked. There may be a good reason for this gem to support Mailjet's Mustache-esque tags, but I figure another issue should be opened if that ever becomes the case.

boardfish commented 2 years ago

Here's that regex: /({[{%](?:\s\w+\s*)?)\w{2,}:(\w+)(?::[\w"]+)?(.*[%}]})/

You'll need to run it multiple times:

loop { break unless template_content.gsub!(regex) }