Packages
Render, compile and convert Handlebars, Mustache, Liquid, Jinja, Django, Nunjucks, Twig, Tera, HubL, Mailchimp and Mustachio templates, with Phoenix engines and sigils.
Current section
Files
Jump to
Current section
Files
README.md
# Braces
<img src="assets/docs/logo.png" alt="A smile wearing braces" width="160">
Render, compile and convert `{{ }}` templates in Elixir.
| Language | Module | Sigil | Converts from | Converts to |
| --- | --- | --- | --- | --- |
| Handlebars | `Braces.Handlebars` | `~HBS` | ✓ | ✓ |
| Mustache | `Braces.Mustache` | `~MUSTACHE` | | |
| Liquid | `Braces.Liquid` | `~LIQUID` | ✓ | ✓ |
| Jinja | `Braces.Jinja` | `~JINJA` | ✓ | ✓ |
| Django | `Braces.Django` | `~DJANGO` | ✓ | ✓ |
| Nunjucks | `Braces.Nunjucks` | `~NUNJUCKS` | ✓ | ✓ |
| Twig | `Braces.Twig` | `~TWIG` | ✓ | ✓ |
| Tera | `Braces.Tera` | `~TERA` | ✓ | ✓ |
| HubL (HubSpot) | `Braces.HubL` | `~HUBL` | ✓ | ✓ |
| Mailchimp merge tags | `Braces.Mailchimp` | `~MAILCHIMP` | ✓ | |
| Mustachio (Postmark) | `Braces.Mustachio` | `~MUSTACHIO` | ✓ | |
Each language renders as its own engine does. Every language has a Phoenix engine, such as
`Braces.Jinja.Phoenix.Engine`.
## Installation
```elixir
def deps do
[
{:braces, "~> 0.1"}
]
end
```
Optional dependencies add integrations:
| Dependency | Adds |
| --- | --- |
| `phoenix_template` | A Phoenix engine for each language |
| `phoenix_live_view` | `Braces.HTML`, to render templates in HEEx |
| `phoenix_html` | `Braces.Handlebars.safe/1` values as safe HTML |
## Render
```elixir
Braces.Jinja.render_to_string("Hi {{ name|upper }}", %{"name" => "<ada>"})
#=> {:ok, "Hi <ADA>"}
Braces.Liquid.render_to_string("{{ items | map: 'price' | sum }}", %{"items" => [%{"price" => 2}, %{"price" => 3}]})
#=> {:ok, "5"}
```
Values are HTML-escaped by default. Pass `escape: false` for text:
```elixir
Braces.Liquid.render_to_string("{{ name }}", %{"name" => "<b>"}, escape: false)
#=> {:ok, "<b>"}
```
Compile once, render many times:
```elixir
template = Braces.Twig.compile!("{% for u in users %}{{ u.name }}{% if not loop.last %}, {% endif %}{% endfor %}")
Braces.render_to_string(template, %{"users" => [%{"name" => "Ada"}, %{"name" => "Grace"}]})
#=> {:ok, "Ada, Grace"}
```
Layouts and includes come from `templates:`:
```elixir
Braces.Jinja.render_to_string(
~s({% extends "base" %}{% block body %}Hi {{ name }}{% endblock %}),
%{"name" => "Ada"},
templates: %{"base" => "<main>{% block body %}{% endblock %}</main>"}
)
#=> {:ok, "<main>Hi Ada</main>"}
```
## Every language
Each example renders with this data:
```elixir
data = %{"name" => "Ada", "items" => [%{"title" => "Tea", "price" => 3}, %{"title" => "Cake", "price" => 5}]}
```
Handlebars:
```elixir
template = Braces.Handlebars.compile!(~S"Hi {{name}}!{{#each items}} {{@index}}:{{title}}{{#if @last}}.{{else}},{{/if}}{{/each}}")
Braces.Handlebars.render_to_string(template, data)
#=> {:ok, "Hi Ada! 0:Tea, 1:Cake."}
```
Mustache:
```elixir
Braces.Mustache.render_to_string(~S"Hi {{name}}!{{#items}} {{title}} ${{price}}{{/items}}{{^items}} Nothing yet.{{/items}}", data)
#=> {:ok, "Hi Ada! Tea $3 Cake $5"}
```
Liquid:
```elixir
Braces.Liquid.render_to_string(~S"Hi {{ name | upcase }}!{% for item in items %} {{ item.title }}{% unless forloop.last %},{% endunless %}{% endfor %} Total: {{ items | map: 'price' | sum }}", data)
#=> {:ok, "Hi ADA! Tea, Cake Total: 8"}
```
Jinja:
```elixir
Braces.Jinja.render_to_string(~S"Hi {{ name|upper }}!{% for item in items %} {{ loop.index }}. {{ item.title }}{% endfor %} Total: {{ items|sum(attribute='price') }}", data)
#=> {:ok, "Hi ADA! 1. Tea 2. Cake Total: 8"}
```
Django:
```elixir
Braces.Django.render_to_string(~S"Hi {{ name|upper }}!{% for item in items %} {{ forloop.counter }}. {{ item.title }}{% empty %} Nothing yet.{% endfor %}", data)
#=> {:ok, "Hi ADA! 1. Tea 2. Cake"}
```
Nunjucks:
```elixir
Braces.Nunjucks.render_to_string(~S"Hi {{ name | upper }}!{% for item in items %} {{ item.title }}{{ ',' if not loop.last }}{% endfor %}", data)
#=> {:ok, "Hi ADA! Tea, Cake"}
```
Twig:
```elixir
Braces.Twig.render_to_string(~S"Hi {{ name|upper }}!{% for item in items %} {{ item.title }}{% if not loop.last %},{% endif %}{% endfor %} {{ (nickname ?? name) ~ ' bought ' ~ items|length }}", data)
#=> {:ok, "Hi ADA! Tea, Cake Ada bought 2"}
```
Tera:
```elixir
Braces.Tera.render_to_string(~S"Hi {{ name | upper }}!{% for item in items %} {{ item.title }}{% if not loop.last %},{% endif %}{% endfor %} Total: {{ items | map(attribute='price') | join(sep='+') }}", data)
#=> {:ok, "Hi ADA! Tea, Cake Total: 3+5"}
```
HubL:
```elixir
Braces.HubL.render_to_string(~S"Hi {{ name|upper }}!{% for item in items %} {{ item.title }}{% unless loop.last %},{% endunless %}{% endfor %}", data)
#=> {:ok, "Hi ADA! Tea, Cake"}
```
Mustachio (Postmark):
```elixir
Braces.Mustachio.render_to_string(~S"Hi {{name}}!{{#each items}} {{title}}{{/each}}{{#vip}} Welcome back.{{/vip}}{{^vip}} Welcome.{{/vip}}", data)
#=> {:ok, "Hi Ada! Tea Cake Welcome."}
```
Mailchimp merge tags, whose names are upper case:
```elixir
Braces.Mailchimp.render_to_string(~S"Hi *|NAME|*! *|IF:COUNT > 1|*You have *|COUNT|* items.*|ELSE:|*You have one item.*|END:IF|*", %{"NAME" => "Ada", "COUNT" => 2})
#=> {:ok, "Hi Ada! You have 2 items."}
```
## Convert
```elixir
Braces.convert("{% for p in people %}{{ p.name | upcase }}{% endfor %}", from: :liquid, to: :jinja)
#=> {:ok, "{% for p in people %}{{ p.name|upper }}{% endfor %}"}
Braces.convert("{% for item in items %}{{ loop.index }}. {{ item.name|title }}{% endfor %}", from: :jinja, to: :handlebars)
#=> {:ok, "{{#each this.items as |item|}}{{add @index 1}}. {{title item.name}}{{/each}}"}
Braces.convert("Hi *|FNAME|*! *|IF:ORDERS > 1|*Welcome back.*|END:IF|*", from: :mailchimp, to: :liquid)
#=> {:ok, ~s(Hi {{ FNAME | escape }}! {% if ORDERS > 1 %}Welcome back.{% endif %})}
```
What the target can't express is an error, with the line and column:
```elixir
Braces.convert("{% for x in xs %}{% cycle 'a', 'b' %}{% endfor %}", from: :liquid, to: :jinja)
#=> {:error, %Braces.ConversionError{message: "Jinja has no cycle outside a loop's loop.cycle on line 1, column 17"}}
```
Handlebars output calls the `Braces.Filters` helpers, so render it with
`helpers: Braces.Filters`.
## Compile into your app
With a sigil, a broken template fails the build at its line:
```elixir
defmodule MyApp.Greetings do
import Braces.Twig, only: :sigils
def hello(data), do: Braces.render!(~TWIG"Hi {{ name ?? 'friend' }}!", data)
end
```
As Phoenix templates:
```elixir
config :phoenix_template, :template_engines,
jinja: Braces.Jinja.Phoenix.Engine,
liquid: Braces.Liquid.Phoenix.Engine
```
Handlebars templates as functions, with helpers and partials:
```elixir
defmodule MyApp.Emails do
use Braces.Handlebars,
helpers: MyApp.EmailHelpers,
# lib/my_app/emails/partials/footer.hbs is {{> footer}}
partials: "emails/partials/*.hbs"
# lib/my_app/emails/welcome.hbs is welcome(data) and welcome(data, opts)
embed_templates "emails/*.hbs"
end
```
## Inspect templates
List the data a template reads, in any language:
```elixir
template = Braces.Liquid.compile!("Hi {{ user.first_name }}! {% for o in orders %}{{ o.id }}{% endfor %}")
Braces.variables(template)
#=> ["orders[].id", "user.first_name"]
```
Find where a template writes values without escaping, for templates your users write:
```elixir
Braces.unescaped_outputs(Braces.Jinja.compile!("{{ name }} {{ bio|safe }}"))
#=> [%{kind: :unescaped, name: "bio", description: "\"bio\" is written without escaping", line: 1, column: 11}]
```
## Render in HEEx
`Braces.HTML` renders a template in any language in HEEx, such as a stored email in a
LiveView:
```heex
<Braces.HTML.render template={@email.body} language={:liquid} data={@recipient} />
```
`mix format` formats `.hbs` files, and `Braces.SendGrid` adds SendGrid's helpers.
## Guides
- [Conversions](guides/conversions.md): what converts, what's written another way, and what
can't be.
- [Layouts and includes](guides/layouts.md)
- [Email providers](guides/email-providers.md): Mailchimp, Postmark, SendGrid, HubSpot and
Klaviyo templates.
- [Liquid to Handlebars](guides/liquid-to-handlebars.md) and
[Handlebars to Liquid](guides/handlebars-to-liquid.md)
- [Phoenix](guides/phoenix.md) and [Swoosh](guides/swoosh.md)
- Handlebars: [rendering](guides/rendering.md),
[helpers and partials](guides/helpers-and-partials.md),
[compiled templates](guides/compiled-templates.md), [parser and AST](guides/parser.md) and
[compatibility with Handlebars.js](guides/compatibility.md)
- [Security](guides/security.md): escaping, and templates from people you don't trust.
## Development
The tests compare Braces with Handlebars.js, LiquidJS and Jinja, so they need Node.js and
Python:
```sh
npm ci --prefix test/js
uv venv test/python/.venv
uv pip install --python test/python/.venv -r test/python/requirements.txt
mix precommit
```
After changing the Handlebars grammar in `lib/braces/handlebars/combinators.ex.exs`, run
`mix compile.nimble` to regenerate the parser.
Rebuild the upstream fixtures from checkouts of each project, then refresh the cases that
still differ:
```sh
node test/js/extract_handlebars_corpus.js <handlebars.js checkout> test/fixtures/handlebars_js_corpus.json
scripts/extract_liquidjs_corpus.sh <liquidjs checkout>
scripts/extract_jinja_corpus.sh <jinja checkout>
MIX_ENV=test mix run scripts/update_pending.exs
```