Braces.Handlebars.parse/2 is a port of Handlebars.js's
handlebars-parser (2.2.4, commit
c3c048f). It gives Braces.Handlebars.AST structs, the same AST as handlebars-parser, with each
node's loc (line from 1, column from 0, in UTF-16 code units as Handlebars.js counts
them).
{:ok, ast} = Braces.Handlebars.parse("Hi {{name}}")
ast.body
#=> [%Braces.Handlebars.AST.ContentStatement{value: "Hi ", …}, %Braces.Handlebars.AST.MustacheStatement{…}]Braces.Handlebars.parse/2applies Handlebars' whitespace control:~strips, and block, comment and partial tags alone on a line take the line with them.ignore_standalone: truekeeps those lines, as Handlebars.js'signoreStandalonedoes.Braces.Handlebars.parse_without_processing/1leaves the text as written.Braces.Handlebars.to_string/1prints an AST as Handlebars source. Print whatparse_without_processing/1gives, or an AST you built: printing whatparse/2gives and parsing the result would apply whitespace control twice.Braces.Handlebars.variables/2lists the data a template reads, as paths such asitems[].name.Braces.Handlebars.Printer.print/1prints an AST as handlebars-parser'sPrintVisitordoes, to compare it with Handlebars.js.
Parse errors
A template that doesn't parse is a Braces.Handlebars.ParseError. Its description is
Handlebars.js's message, and its message adds the template's line with a caret under the
error, as does the CompileError from ~HBS:
if doesn't match each - 1:3
{{#if a}}{{/each}}
^A syntax error has its own description instead, on the line Handlebars.js reports, with the
caret at the start of the tag that doesn't parse, even inside blocks (Handlebars.js points at
the token inside the tag). That includes an {{else}}, {{^}} or {{else if}} its block can't
have, and a {{!-- comment with no --}}. When a block closes with another block's name, or
has an invalid path, before the syntax error, that error is reported instead, as Handlebars.js
does. The caret counts a wide character, such as an emoji or a CJK character, as two columns:
Parse error on line 3, column 2: unexpected "{{foo bar=}}\n{{/each"
{{foo bar=}}
^