Braces.convert/2 converts a template from one language to another:

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("{{#if user}}Hi {{user.name}}{{/if}}", from: :handlebars, to: :twig)
#=> {:ok, ~s<{% if (user or user is same as("0")) %}Hi {{ user.name }}{% endif %}>}

No language is the hub. Each language reads its templates into a neutral representation, Braces.IR, and writes the IR in its own syntax, so any language Braces reads converts to any language it writes.

Languagefrom:to:
Handlebars:handlebars:handlebars
Liquid:liquid:liquid
Jinja:jinja:jinja
Django:django:django
Nunjucks:nunjucks:nunjucks
Twig:twig:twig
Tera:tera:tera
HubL:hubl:hubl
Mailchimp merge tags:mailchimp
Mustachio (Postmark):mustachio

Braces.languages/0 gives the same lists. Each language module has to_ir/2 and, when Braces writes it, from_ir/2, such as Braces.Twig.to_ir/2 and Braces.Liquid.from_ir/2.

What a conversion keeps

The converted template renders what the source renders, with the same data. Where the target can't say what the source says, the conversion is a Braces.ConversionError, with the source's line and column, instead of a template that renders something else.

Truth. Languages disagree on which values are false:

LanguageFalse values
Liquidnil, false
Mustachenil, false, []
Nunjucks (JavaScript)nil, false, 0, ""
Handlebarsnil, false, 0, "", []
Jinja, Django, Tera, HubL (Python)nil, false, 0, "", [], {}
Twig (PHP)nil, false, 0, "", "0", [], {}

The IR records the source's truth, and the target tests a value as the source does: {% if zero %} from Liquid is true when zero is 0, so Jinja gets {% if (zero is defined and zero is not none and zero is not false) %}. Handlebars gets the Braces.Filters truthy helper, {{#if (truthy zero "liquid")}}. Liquid gets the whole test, {% unless x == nil or x == false or x == 0 or … %}, since it has no other way to say it.

Escaping. A value escaped in the source is escaped in the target: Jinja-family targets are written for autoescaping, with |safe (|raw in Twig) on values the source doesn't escape. Liquid engines don't escape, so Liquid is written with | escape on escaped values. Pass escape: false for templates whose values aren't HTML at all, such as text email.

Loops. A loop's index, first, last, length and key are written as the target's loop variables, loop.index, forloop.counter, @index and so on, with an outer loop's where the target has them (forloop.parentloop, loop.parent.loop, @../index).

Filters. Filters and helpers become Braces.Filters calls in the IR, by name, and each target writes them as its own: upcase in Liquid, upper in Jinja, {{upper x}} in Handlebars. Handlebars output needs the Braces.Filters helpers to render: Braces.Handlebars.render(template, data, helpers: Braces.Filters).

What has no syntax in the target

When the target can't write something as the source does, but can get the same result another way, the conversion writes it that way:

  • Variables in Handlebars and Django, which have none. {% assign t = title | upcase %} and {% capture %} are written where they're read: {{upper title}}. A value read inside a loop or #with entered after the assignment still reads what it read where it was set: {{upper @root.title}}, or ../ past the blocks in between. Variables set in a loop in Jinja, which keeps them in the loop, are written the same way.

  • Outer loops' variables in Jinja, HubL, Nunjucks and Tera, which name only the innermost loop. The outer loop saves what its inner loops read: {% set loop1_index = loop.index %}.
  • Grouped conditions in Django, whose {% if %} has no parentheses. not (a or b) is written not a and not b, and a and (b or c) as an {% if %} inside an {% if %}, with the else written in each.
  • Mustache's sections, {{#value}} from Handlebars, which loop over a list, enter a map and keep the context for true. Each language tests for a list its own way: Twig by its keys, Django by x.items and stringformat, Tera by is object.
  • Computed keys in Handlebars, a[key], written (lookup a key), and names Handlebars would call as helpers, written {{this.title}}.
  • + in Django, written with the add filter, and a reversed loop with reversed.
  • {% with %} in Liquid, Handlebars, Nunjucks and Tera, whose names would outlast the block or which have none: its values are written where they're read. Jinja, Django and Twig keep their own {% with %}.
  • Recursive loops, {% for … recursive %} and loop(children), which Jinja reads as a macro that calls itself, written as one in Nunjucks and Twig.
  • {% autoescape %}, which escapes each value in it, or doesn't, as the values are written in any target.

What can't be converted

A Braces.ConversionError names what the target can't say. The common ones:

  • A variable whose value depends on which way an if or a loop went, read where Handlebars or Django would have to write it in place.
  • Includes with a context or arguments, and custom block helpers, in any language.
  • Macros, {% call %}, inline ifs, list and map literals and range outside the Jinja family, and {% break %} and {% continue %} outside Jinja, Tera and Liquid. Twig has no {% call %}, and Django none of them.
  • Grouping in Django output values, and Django's missing -, * and ~.
  • Mustache's sections in Liquid, which can't tell a list from a map.
  • Liquid's blank outside Liquid and Handlebars, and Liquid's tablerow, cycle, increment, ranges, and loop limit and offset.
  • Filters a target has no equivalent for, such as Tera's keyword arguments in Liquid.
  • A sort that ignores case in Twig, Tera and Django, whose sorts tell case apart, and a sort by a key in Twig.
  • Keys read off a computed value, (a ~ b).c, in Liquid and Django.
  • {% filter %} blocks in Handlebars and Django, which can't filter written text, and loop.changed(), loop.previtem and loop.nextitem outside Jinja and HubL, and {% set outer = loop %} outside Jinja, HubL and Nunjucks.
  • Python's slices, xs[1:3], outside Jinja and HubL, and // and ``** in Liquid, Django and Tera.

Known differences

A few values render differently, because the target engine treats the data differently and the template can't see it:

  • Values written as text. A list, a map, a boolean or a float is written the way the target writes it, ['a', 'b'] in Jinja and a,b in Handlebars, True in Python, 1 in Twig and true elsewhere. 4 / 2 is 2.0 in Jinja and 2 elsewhere. From Handlebars to Liquid, lists are joined as JavaScript joins them.

  • Comparing numbers with strings. Braces.Filters' eq, which Handlebars output uses, takes "1" as equal to 1, as Twig does, where Jinja and Liquid don't.

  • Entities. Each engine escapes with its own entities: &#39;, &#x27; or &#34;, the same characters spelled differently.

  • Loops over maps in Python. Jinja and Django loop over a map's keys, and the converted loop over x.items() gives its pairs in the map's order.

  • Maps in Twig. PHP has one empty array for [] and {}, so Twig takes {} as false where Handlebars takes it as true. twig.js takes {} as true.

  • Maps in Liquid. Liquid can't tell a list from a map, so a loop written in Liquid assumes a list, and its key is the index. .size, .first and .last read from Liquid are the filters, not keys of those names.

  • Django's x.items. Django tests a map with x.items is not None, which Django reads as the map's method. Braces.Django's own renderer reads items as a key.

  • Liquid's map and sum take a key, and convert to Jinja's with attribute=; Jinja's map("upper"), which calls a filter, can't be converted to Liquid.

  • Ties in a reversed sort. Jinja's sort(reverse=true) keeps items that sort the same, A and a, in their order; Liquid's sort | reverse turns them around too.

How it's tested

Conversions are tested by rendering the source and the converted template with the same data: with Braces' renderers in the test suite, and, while developing, with Python's Jinja2 and Django, and Node's Nunjucks, twig.js and LiquidJS, for every pair of languages.