# `Braces.Mailchimp`
[🔗](https://github.com/dbernheisel/braces/blob/v0.1.0/lib/braces/mailchimp.ex#L1)

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.

# `compile`

```elixir
@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.Template`s, or a function of a name giving `{:ok, template}`
    or `:error`. They're compiled into it.

# `compile!`

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

Like `compile/2`, raising the error.

# `render`

```elixir
@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`

```elixir
@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`
*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`.

# `convert`

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!`

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

# `to_ir`

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
