Handlebars to Liquid

Copy Markdown View Source

Braces.Handlebars.to_liquid/1 takes what Braces.Handlebars.parse/2 gives, so Handlebars' whitespace control is already applied, and writes Liquid that renders the same text:

"{{#each items}}{{@index}}: {{name}} {{/each}}"
|> Braces.Handlebars.parse!()
|> Braces.Handlebars.to_liquid()
#=> {:ok, "{% for item in items %}{{ forloop.index0 | join: \",\" | escape | … }}: …{% endfor %}"}

It goes by way of Braces.IR, as Braces.convert/2 does with from: :handlebars, to: :liquid (see the Conversions guide). It converts text, {{value}} and {{{value}}} with paths (a.b, a.[0], this, ../), {{#if}}, {{#unless}}, {{#each}} and {{#with}} with {{else}} and {{else if …}}, block parameters, and @index, @first, @last, @key and @root, and the Braces.Filters helpers that Liquid has filters for, such as upper, add, join and eq. Comments are left out. {{> name}} and {{> name null key=value}} become {% include %} and {% render %}, and a partial block with inline partials a {% layout %} (see Layouts and includes).

The Liquid does what Handlebars does, not what Liquid would:

HandlebarsLiquid
{{value}}join: "," writes lists as JavaScript does, then escape and replace escape ", ', ` and = as Handlebars does
{{{value}}}join: ",", unescaped
{{#if x}}Handlebars' falsy values, false, null, "", 0 and [], through {% unless %} and an assign, since Liquid's are only nil and false
{{#with x}}The same, except that 0 isn't empty
{{#each list}}{% for item in list %}, with paths inside it rewritten, since Liquid has no context
{{upper name}}, {{#if (gt n 1)}}name | upcase, {% if n > 1 %}
{{x.length}}x.size, the same for lists and most strings
Text with {{ or {%{{ "{{" }}, {{ "{%" }}
Whitespace-only text in a block body with no output{{ " " }}, since Shopify's Liquid would remove it

to_liquid/1 returns a Braces.ConversionError with the line (from 1) and column (from 0) for helpers Liquid has no filter for, other block helpers, partials with a context or a dynamic name, decorators other than inline partials, {{^…}} and {{#value}} sections, raw blocks, {{this}} at the top, and @index outside #each.

Liquid renders a few values differently from Handlebars.js, which the Liquid can't know about:

  • {{#each}} over an object, which Handlebars iterates by value, or over a string, which it doesn't iterate
  • a.[0] on a string or on an object with a key "0", and keys named size, first or last, which Liquid reads as properties
  • length of an object, which Handlebars reads as a key and Liquid's size counts, and of a string with characters such as emoji, which JavaScript counts as two
  • a #with or #each whose value is the context it's in: Handlebars adds no ../ level for it. The Liquid gets this right for {{#with this}}, not for other paths to the same value.

Names like __joined_1 and __value_1 are the Liquid's own variables, so a top-level value with such a name is a Braces.ConversionError. A loop's element is named item, or item_ when the template reads a value named item.

The tests render the Handlebars with Handlebars.js and the Liquid with Braces, which renders as LiquidJS does: every construct above, the renders in handlebars.js's spec that the subset covers, and random templates from StreamData properties.