Braces.Mailchimp (Braces v0.1.0)

Copy Markdown View Source

Mailchimp's merge tags, *|FNAME|*, rendered as Mailchimp fills them in, with the merge fields as the data, by their tags' names.

iex> Braces.Mailchimp.render_to_string("Hi *|FNAME|*! *|IF:AGE >= 18|*Welcome.*|ELSE:|*Ask a parent.*|END:IF|*", %{"FNAME" => "Ada", "AGE" => 36})
{:ok, "Hi Ada! Welcome."}

Rendering

render/3 renders a template with its data. compile/2 compiles one to render many times, and the ~MAILCHIMP sigil compiles it with your code, so a template that can't be rendered is a CompileError at its line:

import Braces.Mailchimp, only: :sigils
Braces.render(~MAILCHIMP"Hi *|FNAME|*", data)

Braces.Mailchimp.Phoenix.Engine renders .mailchimp files in Phoenix. Braces.convert/2 converts merge tags to the other languages.

What renders

  • Merge fields: *|FNAME|*, *|LIST:COMPANY|*, and *|HTML:BIO|*, written without escaping.
  • *|UPPER:FNAME|*, *|LOWER:…|*, *|TITLE:…|* and *|URL:…|*.
  • *|DATE:d/m/y|* and *|CURRENT_YEAR|*; *|DATE:|* with no format writes F j, Y, "January 5, 2026".
  • *|AUTOMATION:CURRENTPOSITION(TEXTORDINAL)|*, (DIGITORDINAL) and (TEXT).
  • *|IF:VIP|*, *|ELSEIF:AGE>18|*, *|ELSE:|*, *|IFNOT:VIP|* and *|END:IF|*, comparing with =, !=, >, <, >= and <=. A merge field that's a number, or text that is one, is compared as a number. *|IF:NAME|* is true when the field has a value: the number 0 is false and the text "0" is true.

The tags Mailchimp fills in itself, such as *|UNSUB|*, *|ARCHIVE|*, *|LIST:ADDRESS|*, *|MC:SUBJECT|*, *|REWARDS|* or *|AUTOMATION:WORKFLOWADDURL(id)|*, are values too: give them in the data by their tags, %{"UNSUB" => unsubscribe_url}. *|HTML:LIST_ADDRESS_HTML|* reads LIST_ADDRESS_HTML, written as it is. *|MC:DATE|* is a value too, since its format is an account setting.

A tag that isn't a merge tag's name, such as *|not a tag|*, is kept as text, as Mailchimp sends it. Groups (*|INTERESTED:…|*), RSS and feed blocks, polls, *|UNSUB:url|*, and the tags that take [$…] parameters, such as *|DISCOUNT_CODE:[…]|* and *|TWITTER:TWEET [$text=…]|*, are a Braces.ConversionError: they need Mailchimp's own content.

Mailchimp doesn't document its escaping: values are HTML-escaped, except in *|HTML:…|*. Pass escape: false to write them as they are, for a plain-text email.

Summary

Rendering

Compiles Mailchimp into a Braces.Template for render/3, or gives the error that convert/2 gives.

Like compile/2, raising the error.

Renders Mailchimp, or a Braces.Template of it, with data, to iodata.

The ~MAILCHIMP sigil: Mailchimp compiled with your code into a Braces.Template, so a template that doesn't convert is a CompileError at its line. Import it with import Braces.Mailchimp, only: :sigils.

Functions

Converts merge tags to Handlebars source, or gives a Braces.ConversionError with the tag's line and column. Takes escape: false to write values without HTML escaping.

Like convert/2, raising Braces.ConversionError when the merge tags can't be converted.

Reads merge tags into a Braces.IR, for Braces.convert/2. Takes escape: false, as convert/2 does.

Rendering

compile(source, opts \\ [])

@spec compile(String.t(), keyword()) ::
  {:ok, Braces.Template.t()} | {:error, Exception.t()}

Compiles Mailchimp into a Braces.Template for render/3, or gives the error that convert/2 gives.

  • escape: false writes values without HTML escaping.
  • templates: the templates it extends or includes, by name: a map of names to Mailchimp source or Braces.Templates, or a function of a name giving {:ok, template} or :error. They're compiled into it.

compile!(source, opts \\ [])

@spec compile!(String.t(), keyword()) :: Braces.Template.t()

Like compile/2, raising the error.

render(template, data, opts \\ [])

@spec render(String.t() | Braces.Template.t(), term(), keyword()) ::
  {:ok, iodata()} | {:error, Exception.t()}

Renders Mailchimp, or a Braces.Template of it, with data, to iodata.

Takes helpers:, functions for filters Braces doesn't have, as modules that use Braces.Handlebars.Helpers, and, with source, escape: and templates:, as compile/2 does.

render_to_string(template, data, opts \\ [])

@spec render_to_string(String.t() | Braces.Template.t(), term(), keyword()) ::
  {:ok, String.t()} | {:error, Exception.t()}

Like render/3, giving a string.

sigil_MAILCHIMP(arg, modifiers)

(macro)

The ~MAILCHIMP sigil: Mailchimp compiled with your code into a Braces.Template, so a template that doesn't convert is a CompileError at its line. Import it with import Braces.Mailchimp, only: :sigils.

Functions

convert(source, opts \\ [])

Converts merge tags to Handlebars source, or gives a Braces.ConversionError with the tag's line and column. Takes escape: false to write values without HTML escaping.

convert!(source, opts \\ [])

Like convert/2, raising Braces.ConversionError when the merge tags can't be converted.

to_ir(source, opts \\ [])

Reads merge tags into a Braces.IR, for Braces.convert/2. Takes escape: false, as convert/2 does.