Braces.Handlebars.Formatter (Braces v0.1.0)

Copy Markdown View Source

A mix format plugin for Handlebars: .hbs and .handlebars files, and ~HBS sigils.

# .formatter.exs
[
  plugins: [Braces.Handlebars.Formatter],
  inputs: ["{lib,test}/**/*.{ex,exs,hbs}", "*.exs"]
]

It formats the inside of each tag, as Braces.Handlebars.to_string/1 prints it, and leaves the text between tags exactly as it is, since in Handlebars that text is the output: indenting it would change what the template renders.

BeforeAfter
{{ first_name }}{{first_name}}
{{#if vip }} … {{ else }}{{#if vip}} … {{else}}
{{ money total currency = "EUR" }}{{money total currency="EUR"}}
{{& raw}}{{{raw}}}
{{#each items as | item i |}}{{#each items as |item i|}}

Comments are kept as they're written, {{!-- … --}} included.

The result always parses to the same template: when it wouldn't, the source is left as it is. A template that doesn't parse raises its Braces.Handlebars.ParseError, so mix format reports it.

Summary

Functions

Formats Handlebars source, as mix format does with this plugin.

Functions

format(source, opts)

Formats Handlebars source, as mix format does with this plugin.

iex> Braces.Handlebars.Formatter.format("Hi {{ first_name }}!\n{{#if  vip }}VIP{{ else }}{{!-- not yet --}}{{/if}}", [])
"Hi {{first_name}}!\n{{#if vip}}VIP{{else}}{{!-- not yet --}}{{/if}}"