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

Nunjucks templates, Mozilla's Jinja for JavaScript, rendered as Nunjucks renders them.

    iex> Braces.Nunjucks.render_to_string("{% for p in people %}{{ loop.index }}/{{ loop.length }} {{ p.name|upper }} {% endfor %}", %{"people" => [%{"name" => "ada"}, %{"name" => "bo"}]})
    {:ok, "1/2 ADA 2/2 BO "}

## Rendering

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

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

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

## What renders

What `Braces.Jinja` renders, with Nunjucks' differences:

  * `{% elseif %}` is `{% elif %}`, and `===` and `!==` compare.
  * `{% for key, value in object %}` loops over an object's keys and values.
  * Values are true and false as in JavaScript: `[]` and `{}` are true.
  * `default` replaces only a missing value, unless its second argument is true.
  * `{% set %}` in a loop is set after it, as in Nunjucks.
  * Macros and `{% call %}`, inline ifs, lists, objects, `range`, keys of any value, and
    `sort(reverse, caseSensitive, attribute)`, as in `Braces.Jinja`, and
    `{% filter upper %}…{% endfilter %}`.

Values are HTML-escaped, as Nunjucks escapes them with `autoescape`, except with `|safe`.
`{% extends %}`, `{% block %}` with `{{ super() }}`, and `{% include %}` render with the
templates given as `templates:` (see [Layouts and includes](layouts.md)). `{% import %}`,
`{% break %}` and the other tags are a `Braces.ConversionError` with the tag's line and
column.

# `compile`

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

Compiles Nunjucks 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
    Nunjucks 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 Nunjucks, 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_NUNJUCKS`
*macro* 

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

# `convert`

Converts Nunjucks 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 Nunjucks, or gives a `Braces.ConversionError` for what Nunjucks
can't say. The template it writes escapes values as Nunjucks does with autoescaping on:
pass `escape: false` for a template without it.

# `to_ir`

Reads Nunjucks 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*
