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

Twig templates, the Jinja of PHP's Symfony, Drupal and Craft, rendered as Twig renders them.

    iex> Braces.Twig.render_to_string("{% for p in people %}{{ p.name ?? 'someone'|upper }}{% if not loop.last %}, {% endif %}{% endfor %}", %{"people" => [%{"name" => "ada"}, %{}]})
    {:ok, "ada, SOMEONE"}

## Rendering

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

    import Braces.Twig, only: :sigils
    Braces.render(~TWIG"Hi {{ name }}", data)

`Braces.Twig.Phoenix.Engine` renders ``.twig`` files in Phoenix. `Braces.convert/2` converts Twig
to and from the other languages.

## What renders

What `Braces.Jinja` renders, with Twig's names and rules:

  * `a ?? b`, `{{ html|raw }}`, `{{ q|url_encode }}`, `same as`, and `default`, which replaces
    a missing, `null`, `false`, empty string or empty array value, and not `0`.
  * `{% elseif %}`, and `{% for key, value in map %}`.
  * `loop.length`, `loop.revindex`, `loop.revindex0` and `loop.parent.loop`.
  * Tests: `x is null`, `x is empty`, `x is divisible by(3)`, `x is same as(y)`.
  * `{%~ … ~%}`, which strips the spaces and tabs next to the tag, and not the newlines.
  * Values are true and false as in PHP: `0`, `"0"`, `""` and `[]` are false.
  * `{% set %}` in a loop is set after it, as in Twig.
  * `test ? a : b`, `a ?: b`, ranges, `1..5` and `range(1, 5)`, which end with 5, arrays,
    `[1, x]`, and hashes, `{name: x}`.
  * Values written as PHP writes them: `true` is `1`, `false` and `null` nothing, and an
    array `Array`. `|sort` sorts as PHP does, telling case apart.
  * `{% macro name(a, b = 1) %}`, called as `_self.name(x)`, or as `forms.name(x)` after
    `{% import _self as forms %}`.
  * `{% with {a: 1} %}`, `{% apply upper %}…{% endapply %}`, and `{% autoescape %}`,
    `{% autoescape 'html' %}` and `{% autoescape false %}`.

Values are HTML-escaped, as Twig escapes them, except with `|raw`. String interpolation
(`"#{x}"`), `starts with`, `matches`, macros from other templates, `{% embed %}` and the
other tags are a `Braces.ConversionError` with the line and column.
`{% extends %}`, `{% block %}` with `{{ parent() }}`, and
`{% include 'card.html' with {name: value} only %}` render with the templates given as
`templates:`; see [Layouts and includes](layouts.md).

# `compile`

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

Compiles Twig 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
    Twig 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 Twig, 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_TWIG`
*macro* 

The `~TWIG` sigil: Twig 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.Twig, only: :sigils`.

# `convert`

Converts Twig to Handlebars source, or gives a `Braces.ConversionError` with the line
and column of what can't be converted. Takes `escape: false` to write values without
HTML escaping.

# `convert!`

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

# `from_ir`

Writes a `Braces.IR` as Twig, or gives a `Braces.ConversionError` for what Twig
can't say. The template it writes escapes values as Twig does with autoescaping on:
pass `escape: false` for a template without it.

# `to_ir`

Reads Twig into a `Braces.IR`, for `Braces.convert/2`. Takes `escape: false` when
the template's values aren't HTML-escaped.

---

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