Templates from email providers

Copy Markdown View Source

Email providers each have a template language. These modules read their templates, so you can move templates into your app, preview them, and test them. Braces.convert/2 converts them to any language Braces writes, such as Liquid or Jinja:

Braces.convert("Hi *|FNAME|*! *|IF:ORDERS > 1|*Welcome back.*|END:IF|*", from: :mailchimp, to: :liquid)
#=> {:ok, ~s(Hi {{ FNAME | escape }}! {% if ORDERS > 1 %}Welcome back.{% endif %})}

Or keep them in the provider's language, and render them as the provider does:

ProviderLanguageModuleSigil
MailchimpMerge tags, *|FNAME|*Braces.Mailchimp~MAILCHIMP
PostmarkMustachioBraces.Mustachio~MUSTACHIO
SendGridHandlebars, with SendGrid's helpersBraces.Handlebars, Braces.SendGrid~HBS
HubSpotHubL, a JinjaBraces.HubL~HUBL
KlaviyoDjango templatesBraces.Django~DJANGO
Braces.Django.render_to_string("Hi {{ first_name|default:'friend'|title }}!", %{"first_name" => "ada"})
#=> {:ok, "Hi Ada!"}

Each module renders source with render/3, compiles it with compile/2 to render many times, and compiles it with your code with its sigil. Each has a Phoenix engine too, such as Braces.Django.Phoenix.Engine (see Phoenix).

A template that needs the provider's own data, such as a tag for a product feed, is a Braces.ConversionError with the tag's line and column. Text in the template stays as it is: a line holding only a block tag is kept, as the providers keep it.

Mailchimp

Braces.Mailchimp.render_to_string("Hi *|TITLE:FNAME|*! *|IF:ORDERS > 1|*Welcome back.*|END:IF|*", %{"FNAME" => "ada", "ORDERS" => 3})
#=> {:ok, "Hi Ada! Welcome back."}

The data is the merge fields, by their tags: %{"FNAME" => "Ada", "LIST:COMPANY" => "Acme"}. Conditions, ELSEIF, IFNOT, HTML:, UPPER: and the other modifiers, and DATE: render. Groups, RSS and feed blocks, polls and discount codes don't. See Braces.Mailchimp.

Postmark: Mustachio

In Mustachio, {{#section}} renders once, even for a list, and ../ goes up one segment of a path:

Braces.Mustachio.render_to_string("{{#each company.employees}}{{name}} at {{../../name}}. {{/each}}", model)
#=> {:ok, "Ada at Acme. "}

A layout's {{{ @content }}} is the template's HTML: render the layout with it as content:

{:ok, body} = Braces.Mustachio.render_to_string(template, model)
Braces.Mustachio.render(layout, model, data: %{"content" => Braces.Handlebars.safe(body)})

SendGrid

SendGrid's dynamic templates are Handlebars with a few more helpers, equals, notEquals, greaterThan, lessThan, and, or, length, insert and formatDate. Render them with Braces.SendGrid; there's nothing to convert:

template = Braces.Handlebars.compile!(~S|{{#greaterThan (length cart) 0}}{{insert name "default=there"}}, your cart is waiting.{{/greaterThan}}|)
Braces.Handlebars.render_to_string(template, %{"cart" => [%{}]}, helpers: Braces.SendGrid)
#=> {:ok, "there, your cart is waiting."}

HubL and Klaviyo

HubL is Jinja, and Klaviyo's templates are Django's, rendered with Braces.Django: {{ value|filter }}, {% if %} and {% for %}, with the filters and tests of Braces.Filters:

Braces.HubL.render_to_string("{% for item in items %}{{ loop.index }}. {{ item.name|upper }} {% endfor %}", %{"items" => [%{"name" => "tea"}]})
#=> {:ok, "1. TEA "}

A filter Braces.Filters doesn't have, such as HubL's format_date, is yours to write, as a module that uses Braces.Handlebars.Helpers: render with helpers: MyApp.EmailHelpers.

Escaping

Values are HTML-escaped, except where the template asks for them as they are: *|HTML:…|*, {{{…}}} and |safe. HubSpot doesn't escape values: pass escape: false to Braces.HubL.render/3 to render as HubSpot does. Mailchimp and Django take escape: false too, for plain-text email (see Security).