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. defaultreplaces 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, andsort(reverse, caseSensitive, attribute), as inBraces.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). {% import %},
{% break %} and the other tags are a Braces.ConversionError with the tag's line and
column.
Summary
Rendering
Compiles Nunjucks into a Braces.Template for render/3, or gives the error that
convert/2 gives.
Like compile/2, raising the error.
Renders Nunjucks, or a Braces.Template of it, with data, to iodata.
Like render/3, giving a string.
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.
Functions
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.
Like convert/2, raising Braces.ConversionError when the template can't be converted.
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.
Reads Nunjucks into a Braces.IR, for Braces.convert/2. Takes escape: false when
the template's values aren't HTML-escaped.
Rendering
@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: falsewrites values without HTML escaping.templates:the templates it extends or includes, by name: a map of names to Nunjucks source orBraces.Templates, or a function of a name giving{:ok, template}or:error. They're compiled into it.
@spec compile!(String.t(), keyword()) :: Braces.Template.t()
Like compile/2, raising the error.
@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.
@spec render_to_string(String.t() | Braces.Template.t(), term(), keyword()) :: {:ok, String.t()} | {:error, Exception.t()}
Like render/3, giving a string.
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.
Functions
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.
Like convert/2, raising Braces.ConversionError when the template can't be converted.
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.
Reads Nunjucks into a Braces.IR, for Braces.convert/2. Takes escape: false when
the template's values aren't HTML-escaped.