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, anddefault, which replaces a missing,null,false, empty string or empty array value, and not0.{% elseif %}, and{% for key, value in map %}.loop.length,loop.revindex,loop.revindex0andloop.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..5andrange(1, 5), which end with 5, arrays,[1, x], and hashes,{name: x}.- Values written as PHP writes them:
trueis1,falseandnullnothing, and an arrayArray.|sortsorts as PHP does, telling case apart. {% macro name(a, b = 1) %}, called as_self.name(x), or asforms.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.
Summary
Rendering
Compiles Twig into a Braces.Template for render/3, or gives the error that
convert/2 gives.
Like compile/2, raising the error.
Renders Twig, or a Braces.Template of it, with data, to iodata.
Like render/3, giving a string.
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.
Functions
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.
Like convert/2, raising Braces.ConversionError when the template can't be converted.
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.
Reads Twig 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 Twig 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 Twig 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 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.
@spec render_to_string(String.t() | Braces.Template.t(), term(), keyword()) :: {:ok, String.t()} | {:error, Exception.t()}
Like render/3, giving a string.
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.
Functions
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.
Like convert/2, raising Braces.ConversionError when the template can't be converted.
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.
Reads Twig into a Braces.IR, for Braces.convert/2. Takes escape: false when
the template's values aren't HTML-escaped.