Giter Club home page Giter Club logo

mj-hydrate's Introduction

<mj-hydrate>

A powerful mjml component that embed the {{ handlebars }} syntax in your mjml files.

  • Create and reuse UI components for emailing
  • Create advanced layout logic
  • Easily reusable for emails on steroids.

Usage

<mj-hydrate> needs:

  • a .mjml file to include refered with the path attribute
  • data to process the handlebars tag (remember to use {{{ syntax }}} to avoid HTML escaping).

You can pass data in many ways:

Inside the tag:

<!-- index.mjml -->
<mj-hydrate path="path/to/mjml.mjml">Lorem ipsum...</mj-hydrate>

<!-- path/to/mjml.mjml -->
<mj-text>{{{mjHydrateContent}}}</mj-text> 

Inject innerHTML in .mjml file

<mj-hydrate path="path/to/mjml.mjml">Lorem ipsum...</mj-hydrate>

and use in path/to/mjml.mjm

==> <mj-text>Lorem ipsum...</mj-text>

use global data object (only in nodejs)

see Define data section

<mj-hydrate path="path/to/mjml.mjml"></mj-hydrate>

and use in path/to/mjml.mjm

<mj-text>Hello {{user.firstname}} {{user.lastname}}</mj-text> 
==> <mj-text>Hello John Doe</mj-text>

use a specific key of the global data object (only in nodejs)

<mj-hydrate path="path/to/mjml.mjml" source="user"></mj-hydrate>

and use in path/to/mjml.mjm

<mj-text>Hello {{firstname}} {{lastname}}</mj-text> 
==> <mj-text>Hello John Doe</mj-text>

use your own key/value by passing attributes

<mj-hydrate path="path/to/mjml.mjml" firstname="Jane" lastname="Doh"></mj-hydrate>

and use in path/to/mjml.mjm

<mj-text>Hello {{firstname}} {{lastname}}</mj-text> 
==> <mj-text>Hello Jane Doh</mj-text>

use "mapping" attributes to pass a key/value list

<mj-hydrate path="path/to/mjml.mjml" mapping="key:value;foo:baz;"></mj-hydrate>

and use in path/to/mjml.mjm

<mj-text>{{key}} / {{foo}}</mj-text> 
==> <mj-text>value / baz</mj-text>

you can embed <mj-hydrate> in <mj-hydrate>

<mj-hydrate path="path/to/mjml.mjml"></mj-hydrate>

and use in path/to/mjml.mjm

<mj-text>This is a title</mj-text>
<mj-hydrate path="{{layout.header}}"></mj-hydrate>
==>
<mj-text>This is a title</mj-text>
<mj-section>... HEADER ...</mj-section>

you can use JSON.stringified string

Use simple quote around attribute value

The value of the attribute will be JSON.parse(), the value needs to be valid JSON (with double-quoted property). If this method throws an error, the value of the attribute will be used as string.

<mj-hydrate path="path/to/mjml.mjml" myJson='{"key": "a json value", "number":42}'></mj-hydrate>

and use in path/to/mjml.mjm

<mj-text>{{key}} // {{number}}</mj-text> 
==> <mj-text>a json value // 42</mj-text>

# Define data only in nodejs

Before executing the mjml2html(...) method, add some data to the context.

This is only available if you run the nodejs version. Not available in mjml app.

MjHydrate.setData({
    user: {
        firstname: "John",
        lastname: "Doe",
        email: "[email protected]"
    },
    layout: {
        header: "inc/header.mjml",
        footer: "inc/footer.mjml",
    }
    
})

mjml2html(...)

Limitations

<mj-hydrate> have some limitations as it works like an <mj-include> but it's not treated as one by the MJML XML Parser.

Then each <mj-hydrate> tag is treated by the parser in the XML flow, not before the other tags.

The MJML XML Parser seems to process the <mj-include> tags before the other tag.

Known bugs

  • bad computation of <mj-column> width. To resolve the issue, specify the "width" attribute.

Usefull links

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.