View Source MjmlEEx.Component behaviour (MJML EEx v0.2.0)

This module allows you to define a reusable MJML component that can be injected into an MJML template prior to it being rendered into HTML. To do so, create an MjmlEEx.Component module that looks like so:

defmodule HeadBlock do
  use MjmlEEx.Component

  @impl true
  def render(_opts) do
    """
    <mj-head>
      <mj-title>Hello world!</mj-title>
      <mj-font name="Roboto" href="https://fonts.googleapis.com/css?family=Montserrat:300,400,500"></mj-font>
    </mj-head>
    """
  end
end

With that in place, anywhere that you would like to use the component, you can add: <%= render_component HeadBlock %> in your MJML EEx template.

You can also pass options to the render function like so:

defmodule HeadBlock do
  use MjmlEEx.Component

  @impl true
  def render(opts) do
    """
    <mj-head>
      <mj-title><%= opts[:title] %></mj-title>
      <mj-font name="Roboto" href="https://fonts.googleapis.com/css?family=Montserrat:300,400,500"></mj-font>
    </mj-head>
    """
  end
end

And calling it like so: <%= render_component(HeadBlock, title: "Some really cool title") %>

Link to this section Summary

Callbacks

Returns the MJML markup for the component as a string.

Link to this section Callbacks

@callback render(opts :: keyword()) :: String.t()

Returns the MJML markup for the component as a string.