-
Notifications
You must be signed in to change notification settings - Fork 961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
If, else, for and arguments #1457
Comments
Ah yeah I understand, so we will continue using Jinja2, thanks! |
+1 on this...a very barebones if-else conditional (like vue.js's v-if and v-else) would be really great. |
You can do it with bare javascript, there is no need for a template language. const shouldClick = Math.random() > 0.5
console.log(`
<mjml>
<mj-body>
<mj-button font-family="Helvetica" background-color="#f45e43" color="white">
${shouldClick ? 'Don\'t click me!' : 'Click me!'}
</mj-button>
</mj-body>
</mjml>
`) |
Here's a solution that worked for me (after lots of trial and error). I am using the MJML app:
|
Here is my version using handlebars.js
|
@codenamezjames , it also integrates nicely with typescript, type safe props, easy components reuse without extra configuration. |
I have this same issue. It isn't that I don't want to use my own templating it is that the mjml preprocessing strips my tags. <%= if true do %>
<mj-text>Hello</mj-text>
<% end %> In this case none of those lines are in the output. <%= if true do %>
<p>Hello</p>
<% end %> In this case the mjml processor preserves my conditional block. I found a workaround with the help of @chadfennell. <!-- <%= if true do %> -->
<mj-text>Hello</mj-text>
<!-- <% end %> --> |
@adkron you should pass first through template engine, and get final mjml, then pass final mjml through mjml I think you are doing mjml, then template engine |
You can just wrap them into mj-raw instead of comment blocks too |
Oh, I run it through the mjml at compile-time, and all the other tags I have are dynamic at runtime. I do the mjml engine first because the template engine I use compiles the templates into functions at compile time so running it through mjml at runtime would be slower and outside the norm for my system. |
I found this example
UPDATE!
Beware of possible problems with minifying template tags like this according to docs. |
To surround twig tags when generating twig files .mjmlconfig.js module.exports = options and in cli |
Before you decide on templating engine, here is a plain JS: import { escape } from 'html-escaper'
const condition = (cond, then) => cond ? then : ''
export default ({
title,
list = []
}) => `
<mjml>
<mj-body>
${condition(title, `
<mj-text>${escape(title)}</mj-text>
<mj-divider />
`)}
${list.map(item => `<mj-text>${escape(item.label)}</mj-text>`)}
</mj-body>
</mjml>
` |
We want to give our end users the ability to edit their own transactional email templates, but some of the data being provided to the template is optional. I'm using https://github.com/zalify/easy-email as the client-facing template editor. |
If you use easy-email, integrating with other template engines is quite straightforward. If you simply want to provide developers with an MJML editing experience, you can try smart-mjml. |
Is your feature request related to a problem? Please describe.
I'm currently working on a project with a friend and we have started creating some email template for different use cases. Some of this templates are very flexible to contain things like dynamic lists that are filled by the user input. So we came up with the problem, that MJML has no tags or something like that to loop through a set of arrays items or even render a simple string in a tag. We program our API in python and using jinja2 templating, therefore we need to pass the pre-rendered jinja2 template with jinja2 if else clauses and variables to MJML CLI. This is a step we would like to avoid.
Describe the solution you'd like
It would be nice if MJML supports something that allows passing arguments to the template that will be rendered. e.g.:
Describe alternatives you've considered
Currently, as I have written above, we use Jinja2 (flask) to prerender the templates and pass it to MJML CLI. Additionally a Python renderer would be awesome too.
The text was updated successfully, but these errors were encountered: