Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MJML - Template Interpolation, Dynamic Data, Context

After a lot of searches, I am having difficulties in finding how:

  1. MJML handles dynamic data and template interpolations

I was expecting something like:

import { mjml2html } from 'mjml';

const context = {
  message: 'Hello World'
};

const view = mjml2html(template, context);
<mjml>
  <mj-body>
    <mj-container>
      <mj-section>
        <mj-column>
          <mj-text>{message}</mj-text>
        </mj-column>
      </mj-section>
    </mj-container>
  </mj-body>
</mjml>
like image 248
Hitmands Avatar asked Mar 30 '17 07:03

Hitmands


People also ask

How do I convert MJML to HTML?

Click on "Compile to HTML" and look at the right panel to see how your email will look like on different devices. When you are done editing your MJML, click on the "HTML" toggle button on the top left of your screen to see the responsive HTML generated by the engine: all you have to do now is save it!

What is MJML?

What is MJML? MJML stands for Mailjet Markup Language and originated from a group of developers at Mailjet (now Pathwire) who set out to help developers code emails in a simpler and more efficient way. So, they developed a markup language that simplifies the complexity of responsive HTML and automatically generates it.

How do I add CSS to MJML?

mj-style. This tag allows you to set CSS styles that will be applied to the HTML in your MJML document as well as the HTML outputted. The CSS styles will be added to the head of the rendered HTML by default, but can also be inlined by using the inline="inline" attribute.


1 Answers

MJML doesn't handle any templating. If you want templates, use a template engine such as handlebars to render to MJML.

import { compile } from 'handlebars';
import { mjml2html } from 'mjml';

const template = compile(`
<mjml>
  <mj-body>
    <mj-container>
      <mj-section>
        <mj-column>
          <mj-text>{{message}}</mj-text>
        </mj-column>
      </mj-section>
    </mj-container>
  </mj-body>
</mjml>
`);
const context = {
    message: 'Hello World'
};
const mjml = template(context);
const html = mjml2html(mjml);
like image 143
Ben Fortune Avatar answered Sep 20 '22 14:09

Ben Fortune