MJML - 模板插值,动态数据,上下文

时间:2017-03-30 07:49:06

标签: node.js reactjs serverside-rendering react-server mjml

经过大量搜索, 我很难找到:

  1. MJML处理动态数据和模板插值
  2. 我期待的是:

    import { mjml2html } from 'mjml';
    
    let context = {
      message: 'Hello World'
    };
    
    let 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>
    

1 个答案:

答案 0 :(得分:16)

MJML不处理任何模板。如果您需要模板,请使用模板引擎(如把手)渲染到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);