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

Django templates, rendered as Django renders them.

    iex> Braces.Django.render_to_string("{% for p in people %}{{ forloop.counter }}. {{ p.name|default:'?'|title }} {% empty %}none{% endfor %}", %{"people" => [%{"name" => "ada"}, %{}]})
    {:ok, "1. Ada 2. ? "}

## Rendering

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

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

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

## What renders

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

  * `{{ items.0.name }}`, and filters with `:` arguments: `{{ total|floatformat:2 }}`,
    `{{ date|date:'F j, Y' }}`, `{{ x|default:'none' }}`, `{{ x|default_if_none:'none' }}`.
  * `{% for %}` with `{% empty %}`, and `{% for key, value in map.items %}`;
    `forloop.counter`, `counter0`, `revcounter`, `revcounter0`, `first`, `last` and
    `parentloop`.
  * `{% comment %}` and `{% verbatim %}`.
  * `dictsort:"key"` and `dictsortreversed:"key"`.
  * `{% with a=1 b=x %}` and `{% with x as a %}`, and `{% autoescape on %}` and
    `{% autoescape off %}`.

Values are written as Python writes them, `True` and `['a', 1]`, and HTML-escaped, as Django escapes them, except with `|safe`. `default` replaces any
value that's false, as in Django.

`{% extends %}`, `{% block %}` with `{{ block.super }}`, and
`{% include "card.html" with name=value only %}` render with the templates given as
`templates:` (see [Layouts and includes](layouts.md)). `{% cycle %}`,
`{% load %}` and the other tags are a `Braces.ConversionError` with the tag's line and
column. Klaviyo's templates are Django's:
render them with this module. Klaviyo's own tags, such as `{% unsubscribe %}`, `{% web_view %}`
and `{% current_year %}`, are a `Braces.ConversionError` too; its filters, such as
`lookup` and `format_date_string`, are in `Braces.Filters`.

# `compile`

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

Compiles Django 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
    Django 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 Django, 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_DJANGO`
*macro* 

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

# `convert`

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

# `to_ir`

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