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

Templates written in braces, `{{ }}`, in Elixir: rendered, compiled into your app, and
converted from one language to another.

| Language | Module | Sigil | `convert/2` reads | `convert/2` writes |
| --- | --- | --- | --- | --- |
| Handlebars | `Braces.Handlebars` | `~HBS` | `:handlebars` | `:handlebars` |
| Mustache | `Braces.Mustache` | `~MUSTACHE` | | |
| Liquid | `Braces.Liquid` | `~LIQUID` | `:liquid` | `:liquid` |
| Jinja | `Braces.Jinja` | `~JINJA` | `:jinja` | `:jinja` |
| Django | `Braces.Django` | `~DJANGO` | `:django` | `:django` |
| Nunjucks | `Braces.Nunjucks` | `~NUNJUCKS` | `:nunjucks` | `:nunjucks` |
| Twig | `Braces.Twig` | `~TWIG` | `:twig` | `:twig` |
| Tera | `Braces.Tera` | `~TERA` | `:tera` | `:tera` |
| HubSpot's HubL | `Braces.HubL` | `~HUBL` | `:hubl` | `:hubl` |
| Mailchimp merge tags | `Braces.Mailchimp` | `~MAILCHIMP` | `:mailchimp` | |
| Mustachio, Postmark's language | `Braces.Mustachio` | `~MUSTACHIO` | `:mustachio` | |

Each language renders as its own engine does: its module's `render/3` renders source, and
`compile/2` or its sigil, `import Braces.Jinja, only: :sigils`, gives a template for
`render/3`. Each has a Phoenix engine, such as `Braces.Jinja.Phoenix.Engine` (see the
[Phoenix](phoenix.md) guide). See the [Email providers](email-providers.md) guide for
templates moved from an email provider, and `Braces.SendGrid` for SendGrid's Handlebars
helpers.

## Converting

`convert/2` converts a template from one language to another, by way of `Braces.IR`: the
source language reads the template into the IR, and the target language writes it. The
conversion keeps the source's meaning, such as which values are true and how values are
escaped, or it's a `Braces.ConversionError` saying what the target can't say. See the
[Conversions](conversions.md) guide.

    iex> Braces.convert("{% for p in people %}{{ p.name }}{% endfor %}", from: :liquid, to: :jinja)
    {:ok, "{% for p in people %}{{ p.name }}{% endfor %}"}

# `language`

```elixir
@type language() ::
  :handlebars
  | :liquid
  | :jinja
  | :django
  | :nunjucks
  | :twig
  | :tera
  | :hubl
  | :mailchimp
  | :mustachio
```

A language `convert/2` reads or writes.

# `convert`

```elixir
@spec convert(String.t(), keyword()) :: {:ok, String.t()} | {:error, Exception.t()}
```

Converts `source` from one language to another: `from:` and `to:` name them. Mailchimp and
Mustachio are read but not written. Takes `escape: false` for a template whose values
aren't HTML-escaped, and, from Handlebars, `helpers:`, the names of the helpers the template
calls without arguments.

    iex> Braces.convert("{% if user %}Hi {{ user.name }}{% endif %}", from: :jinja, to: :twig)
    {:ok, ~s<{% if (user or user is same as("0")) %}Hi {{ user.name }}{% endif %}>}

    iex> Braces.convert("{% assign t = title | upcase %}<h1>{{ t }}</h1>", from: :liquid, to: :handlebars)
    {:ok, "<h1>{{upper this.title}}</h1>"}

    iex> {:error, error} = Braces.convert("{% for p in people %}{% assign last = p %}{% endfor %}{{ last.name }}", from: :liquid, to: :handlebars)
    iex> error.message
    "last has the value of whichever way an if or a loop went on line 1, column 54"

# `convert!`

```elixir
@spec convert!(String.t(), keyword()) :: String.t()
```

Like `convert/2`, raising the error.

# `languages`

```elixir
@spec languages() :: %{read: [language()], write: [language()]}
```

The languages `convert/2` reads, and those it writes.

# `render`

```elixir
@spec render(struct(), term(), keyword()) :: {:ok, iodata()} | {:error, Exception.t()}
```

Renders a compiled template in any language with `data`, to iodata: a `Braces.Template`, a
`Braces.Handlebars.Template` or a `Braces.Mustache.Template`, from a language module's
`compile/2` or its sigil. Takes the options of the language's `render/3`.

    iex> import Braces.Liquid, only: :sigils
    iex> Braces.render_to_string(~LIQUID"Hi {{ name | upcase }}!", %{"name" => "ada"})
    {:ok, "Hi ADA!"}

# `render!`

```elixir
@spec render!(struct(), term(), keyword()) :: iodata()
```

Like `render/3`, raising the error.

# `render_to_string`

```elixir
@spec render_to_string(struct(), term(), keyword()) ::
  {:ok, String.t()} | {:error, Exception.t()}
```

Like `render/3`, giving a string.

# `unescaped_outputs`

```elixir
@spec unescaped_outputs(struct(), keyword()) :: [map()]
```

Finds where a compiled template, in any language, writes a value without HTML escaping:
for templates that people you don't fully trust write, such as email templates your users
edit. Gives a list in the template's order, of maps with the `kind`, `:unescaped`, the
`name` written, a `description`, and the `line` (from 1) and `column` (from 0), which are
`nil` for Mailchimp and Mustachio, whose templates don't keep them. An empty list means every value the
template writes is escaped.

    iex> template = Braces.Jinja.compile!("Hi {{ name }}\n{{ bio|safe }}")
    iex> for found <- Braces.unescaped_outputs(template),
    ...>     do: {found.name, found.line, found.column}
    [{"bio", 2, 0}]

A value is listed when it's written as it is: Jinja's and Django's `safe`, `{% autoescape
false %}`, Liquid's `raw`, Mustache's `{{{value}}}`, and Mailchimp's `*|HTML:NAME|*`.
Text the template writes itself isn't listed, nor is what Liquid's `capture` holds, or a
macro writes, as their values were escaped where they were written. The template's own text
is HTML too, so render templates you don't trust where their HTML can't do harm, such as an
`<iframe sandbox>`.

A Handlebars template is read by `Braces.Handlebars.unescaped_outputs/2`, whose options this
takes, and which also lists block helpers.

# `variables`

```elixir
@spec variables(struct(), keyword()) :: [String.t()]
```

Lists the data a compiled template reads, in any language, as paths from the data's root,
sorted: what a form or a sample needs to fill in, or a check that data has before it's sent.

    iex> import Braces.Liquid, only: :sigils
    iex> Braces.variables(~LIQUID"""
    ...> Hi {{ user.first_name }}!
    ...> {% for item in order.items %}{{ item.name }}: {{ item.price | money }}{% endfor %}
    ...> {% assign shipping = order.shipping %}{{ shipping.city }}
    ...> """)
    ["order.items[].name", "order.items[].price", "order.shipping.city", "user.first_name"]

A path's segments are joined with dots, and `[]` stands for each element of a list. Only
the deepest paths are listed, since `user.name` implies `user`. Paths are followed through
the variables that loops, `with` and `assign` set from the data. A variable set to anything
else, such as a filter's result or a macro's parameter, isn't data, and neither is a loop
variable such as `loop.index`.

A Handlebars template is read by `Braces.Handlebars.variables/2`, whose options this takes.

---

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