Liquid to Handlebars

Copy Markdown View Source

Braces.convert/2 converts Liquid to Handlebars, as Braces.Liquid.convert/2 does:

Braces.convert(~s(Hi {{ first_name | default: "there" }}!), from: :liquid, to: :handlebars)
#=> {:ok, ~s(Hi {{default first_name "there" empty=true}}!)}

The Handlebars calls the helpers of Braces.Filters for Liquid's filters and comparisons, so render it with helpers: Braces.Filters. It goes by way of Braces.IR (see the Conversions guide).

Braces renders Liquid itself, as LiquidJS does, without converting it: Braces.Liquid.render/3, the ~LIQUID sigil and Braces.Liquid.Phoenix.Engine. The tests check this against LiquidJS 10.31 with its own test suite, in test/fixtures/liquidjs; the cases Braces doesn't render the same yet are listed in pending.json.

Converted

LiquidHandlebars
TextText, with {{ escaped as \{{.
{{ x }}, {{ x.y | upcase }}, {% echo x %}{{x}}, {{upper x.y}}, HTML-escaped. With escape: false, {{{x}}}, as Liquid doesn't HTML-escape output.
{% if %}, {% elsif %}, {% else %}, {% unless %}{{#if}}, {{else if}}, {{else}}, with Liquid's truth: only nil and false are false.
{% case %} / {% when %} / {% else %}One {{#if}} per when, since Liquid renders every when that matches, and an {{#unless}} for the else.
{% for %} with limit, offset, reversed, ranges, forloop and {% else %}{{#each … as |item|}}…{{else}}…{{/each}}, with @index, @first and @last.
{% assign %}, {% capture %}Their values, put where they're used.
{% comment %}, {% # %}, {% doc %}Nothing.
{% raw %}Its text, escaped.
{%-, -%}, {{-, -}}Applied.
{% liquid %}Its tags, converted like any others.
{% layout %}, {% block %}, {% include %}, {% render %}The templates given as templates:, put in place (see Layouts and includes).

Escaping

Liquid writes values as they are, so a value with HTML in it is HTML in the page. Braces escapes them, as Handlebars escapes {{value}}, unless given escape: false:

Braces.convert("Hi {{ name }}", from: :liquid, to: :handlebars)
#=> {:ok, "Hi {{name}}"}

Braces.convert("Hi {{ name }}", from: :liquid, to: :handlebars, escape: false)
#=> {:ok, "Hi {{{name}}}"}
  • Outputs the Liquid escapes with a last escape filter aren't escaped again.

  • Captured variables keep their markup. A variable the template captures, and never assigns or loops over, is written as it is when it's output with no filters, and the outputs inside the capture are escaped. So a button built with capture keeps its link:

    {% capture button %}<a href="{{ url }}">{{ label }}</a>{% endcapture %}{{ button }}

    A captured variable that's also assigned or looped over, or output with filters, is escaped where it's output, its markup included.

Not converted

These are a Braces.ConversionError with their line and column, as Handlebars has nothing like them. Braces.Liquid renders them.

  • {% cycle %}, {% increment %}, {% decrement %} and {% tablerow %}
  • {% break %} and {% continue %}
  • a variable whose value depends on which way an if or a loop went, read after it

{% ifchanged %} and {% render … for %} are an error in Liquid too, as Braces doesn't render them.