2

I evaluate mjml as a tool for making my days less horrible when I need to do something with transactional emails and there is one feature that I can't find any informations of but I'm just sure there is a way for people to do it right.

Reusable layout.

I used to write emails in pug and it was basic feature there. I know mjml wants to be a framework and not template engine but they do have "mj-include" and layout is kind of reverse include.

Pseudo code explaining what I would like to achieve:

layout.mjml

<mjml>
  <mj-head>...</mj-head>
  <mj-body>
     ... some header
     <mj-this-is-where-content-comes><</mj-this-is-where-content-comes>
     ... some footer
  </mj-body>
</mjml>

welcome-user.mjml

<mj-layout src="./layout.mjml">
  <mj-text>
     Hi user!
  </mj-text>
</mj-layout>

Based on my reading I see few ways how people could deal with it:

  • mj-include <= documentation examples stands on that, still requires lots of copy and paste for each template

  • wrapping template with layout in compile process <= I'm loosing the power of mjml tools in this case, I could though set-up watchers that compile and wrap my code in layout for each change

  • custom component <= seems like the best option but aren't reusable layouts such a common case that if it would be a solution then by now I would probably find custom component for it

Paweł Wszoła
  • 1,096
  • 1
  • 9
  • 21
  • 2
    This seems like way more trouble than it's worth. Why not just create functions that create your templates using string interpolation? – Pytth Jan 31 '18 at 18:39
  • Yes, so It's pretty much second option and yeah, it's the easiest DRY method and if I needed it done for tomorrow I would go with it though I think it's not ideal and if such doesn't exists, it's perfect opportunity for pull request. – Paweł Wszoła Jan 31 '18 at 19:43
  • 1
    just saw this question and decided to go with mj-include for my own use case, what did you mean by a lot of C&P? – devonj May 24 '18 at 21:04
  • As pointed out by @devonj, maybe https://documentation.mjml.io/#mj-include can help you avoid some duplicated code. – Ezequias Dinella Apr 10 '23 at 23:50

0 Answers0