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

Jinja templates, rendered as Jinja renders them.

    iex> Braces.Jinja.render_to_string("{% for user in users %}{{ loop.index }}. {{ user.name|title }} {% endfor %}", %{"users" => [%{"name" => "ada"}]})
    {:ok, "1. Ada "}

    iex> Braces.Jinja.render_to_string("{{ name|default('friend')|title }}", %{})
    {:ok, "Friend"}

## Rendering

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

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

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

## What renders

  * Values: `{{ user.name }}`, `{{ user['first name'] }}`, `{{ items[0] }}`, with filters,
    `{{ name|upper }}`, `{{ text|truncate(40) }}`, and expressions: `a ~ b`, `a + b * 2`,
    `-a`, `'x' in list`, comparisons, `and`, `or` and `not`, `a if test else b`, lists,
    `[1, x]`, tuples, `(1, 2)`, `(1,)` and `()`, written as Python writes them, maps, `{'a': x}`, and `range(stop)`, `range(start, stop)`
    and `range(start, stop, step)`, which stop before `stop`, as Python's do.
  * Keys of any value, `{'a': 1}.a`, `[1, 2][0]` and `(a ~ b).c`, and keys the template
    computes, `user[field]`. Python's numbers, `1_000`, `1e20` and `0x1F`, strings'
    escapes, `'\n'` and `'\x41'`, and strings next to each other, `"a" "b"`. `+` joins
    strings and lists, as in Python.
  * `{% if %}`, `{% elif %}`, `{% else %}`; `{% for item in items %}` with `{% else %}`, and
    `{% for key, value in map.items() %}`, `{% for a, b, c in rows %}`, loops over a
    string's characters, and loop filters, `{% for x in xs if x.ok %}`;
    `loop.index`, `loop.index0`, `loop.first`, `loop.last`, `loop.length`, `loop.revindex`,
    `loop.revindex0`, `loop.previtem`, `loop.nextitem`, `loop.changed(x)`, and `loop` as a
    value, `{% set outer = loop %}`. A loop's names have what they had before it after it.
  * Recursive loops, `{% for item in tree recursive %}…{{ loop(item.children) }}`, with
    `loop.depth` and `loop.depth0`.
  * `{% with a = 1, b = x %}`, whose names, and those set in it, last until `{% endwith %}`;
    `{% filter upper %}…{% endfilter %}`; and `{% autoescape true %}` and
    `{% autoescape false %}`.
  * Python's operators: chained comparisons, `1 < x < 5`, `//`, `**`, and slices,
    `xs[1:3]`, `xs[::-1]`, and negative indexes, `xs[-1]`.
  * Tests: `x is defined`, `x is not divisibleby 3`, the comparisons, `x is eq 1`,
    `x is lt 3` and `x is in list`, and the others of `Braces.Filters`.
  * `{% set %}` and `{% set %}…{% endset %}`, scoped as Jinja scopes them: one set in a loop
    is the loop's.
  * `{% macro name(a, b=1) %}`, called as `name(x, b=2)`, with the template's variables
    and its own; `{% call name() %}…{% endcall %}` and `{% call(item) name() %}`, whose body
    the macro writes with `caller()`, or `caller(item)`. What a macro writes isn't escaped
    again.
  * `{% break %}` and `{% continue %}`, as Jinja's `loopcontrols` extension has them.
  * `{# comments #}`, `{% raw %}`, and whitespace control, `{%- … -%}`, and `{%+ … +%}`,
    which keeps what Braces never strips.
  * `{% extends "base.html" %}` and `{% block %}`, with `{{ super() }}`, and
    `{% include "card.html" %}`, `with context` or `without context`, from the templates
    given as `templates:`. See [Layouts and includes](layouts.md).

Filters are the functions of `Braces.Filters`, such as `sort(reverse, case_sensitive,
attribute)`; give `helpers:` for others. Values are true and false as in Python: `0`, `""`,
`[]` and `{}` are false. They're written as Python writes them, `True`, `2.0`, `1e+20`,
`['a', 1]` and `{'k': None}`, and `/` divides to a float, as in Jinja. `nil` is written as
nothing, as a missing value is, except in a list or a map, where it's `None`.

Values are HTML-escaped, except with `|safe`, as Jinja does with autoescaping on. Pass
`escape: false` to render as Jinja does without it, where `|escape` still escapes.

`{% import %}` and `{% from … import %}`, `{% include … ignore missing %}` and the
other tags are a `Braces.ConversionError` with the tag's
line and column. Converting to a language without them, macros, inline ifs, literals and
loop controls are a `Braces.ConversionError` too. `Braces.Django`,
`Braces.Nunjucks`, `Braces.Twig`, `Braces.Tera` and `Braces.HubL` render
Jinja's relatives.

# `compile`

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

Compiles Jinja 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
    Jinja 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 Jinja, 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_JINJA`
*macro* 

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

# `convert`

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

# `to_ir`

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