Braces.Tera (Braces v0.1.0)

Copy Markdown View Source

Tera templates, the Jinja of Rust, rendered as Tera renders them.

iex> Braces.Tera.render_to_string(~s|{{ name \| default(value="friend") \| title }}, {{ day \| date(format="%d %B") }}|, %{"day" => ~D[2026-12-04]})
{:ok, "Friend, 04 December"}

Rendering

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

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

Braces.Tera.Phoenix.Engine renders .tera files in Phoenix. Braces.convert/2 converts Tera to and from the other languages.

What renders

What Braces.Jinja renders, with Tera's differences:

  • Filters take keyword arguments: default(value="x"), replace(from="a", to="b"), join(sep=", "), split(pat=","), round(precision=2), and truncate(length=20), which keeps 20 characters and adds ….
  • date(format="%Y-%m-%d") formats with strftime's format.
  • {% for key, value in map %} loops over a map's keys and values.
  • Its tests: starting_with, ending_with, containing, object, iterable.
  • default replaces only a missing value.
  • {% set %} sets a variable for the loop it's in, and {% set_global %} for the template.
  • sort(attribute="name"), telling case apart.
  • range(end=5), with start= and step_by=, arrays, [1, x], and {% break %} and {% continue %}.

Values are HTML-escaped, as Tera escapes them in HTML files, except with | safe. Macros, which Tera imports from other files, and the other tags are a Braces.ConversionError with the tag's line and column. {% extends %}, {% block %} with {{ super() }}, and {% include %} render with the templates given as templates:; see Layouts and includes.

Summary

Rendering

Compiles Tera into a Braces.Template for render/3, or gives the error that convert/2 gives.

Like compile/2, raising the error.

Renders Tera, or a Braces.Template of it, with data, to iodata.

The ~TERA sigil: Tera 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.Tera, only: :sigils.

Functions

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

Reads Tera into a Braces.IR, for Braces.convert/2. Takes escape: false when the template's values aren't HTML-escaped.

Rendering

compile(source, opts \\ [])

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

Compiles Tera 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 Tera source or Braces.Templates, or a function of a name giving {:ok, template} or :error. They're compiled into it.

compile!(source, opts \\ [])

@spec compile!(String.t(), keyword()) :: Braces.Template.t()

Like compile/2, raising the error.

render(template, data, opts \\ [])

@spec render(String.t() | Braces.Template.t(), term(), keyword()) ::
  {:ok, iodata()} | {:error, Exception.t()}

Renders Tera, 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(template, data, opts \\ [])

@spec render_to_string(String.t() | Braces.Template.t(), term(), keyword()) ::
  {:ok, String.t()} | {:error, Exception.t()}

Like render/3, giving a string.

sigil_TERA(arg, modifiers)

(macro)

The ~TERA sigil: Tera 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.Tera, only: :sigils.

Functions

convert(source, opts \\ [])

Converts Tera 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!(source, opts \\ [])

Like convert/2, raising Braces.ConversionError when the template can't be converted.

from_ir(ir, opts \\ [])

Writes a Braces.IR as Tera, or gives a Braces.ConversionError for what Tera can't say. The template it writes escapes values as Tera does with autoescaping on: pass escape: false for a template without it.

to_ir(source, opts \\ [])

Reads Tera into a Braces.IR, for Braces.convert/2. Takes escape: false when the template's values aren't HTML-escaped.