Rendering templates

Copy Markdown View Source

Braces.Handlebars.compile/2 parses a template once; render it as many times as you like with Braces.Handlebars.render/3:

template = Braces.Handlebars.compile!("<ul>{{#each items}}<li>{{name}}</li>{{else}}None{{/each}}</ul>")

Braces.Handlebars.render_to_string(template, %{"items" => [%{"name" => "Tea & cake"}]})
#=> {:ok, "<ul><li>Tea &amp; cake</li></ul>"}

Braces.Handlebars.render(template, %{items: [%{name: "Ada"}]})
#=> {:ok, iodata}

{:ok, data} = Braces.Handlebars.decode_json(~s({"items": [{"name": "Bo"}]}))
Braces.Handlebars.render_to_string(template, data)
#=> {:ok, "<ul><li>Bo</li></ul>"}
FunctionReturns
Braces.Handlebars.render/3{:ok, iodata} or {:error, %Braces.Handlebars.RenderError{}}
Braces.Handlebars.render_to_string/3{:ok, binary} or {:error, %Braces.Handlebars.RenderError{}}
Braces.Handlebars.render!/3iodata, or raises Braces.Handlebars.RenderError

render/3 reads the template's AST as it renders, as it does an AST from Braces.Handlebars.parse/2: no code is generated and no atoms are created, so it's safe for templates your users write. A template you have when your code compiles can be compiled into Elixir code instead (see Compiled templates).

Data

Data is looked up and written as Handlebars.js does:

  • Each key is looked up in maps, then in struct fields. A key is tried as a string first, then as an atom that already exists, so decoded JSON and atom-keyed maps both work. Lists have length and indexes (items.[0]), and strings have length.
  • A list of {key, value} tuples with string keys is an object whose keys keep their order. Braces.Handlebars.decode_json/1 decodes JSON objects into such lists (and {} into %{}), so #each walks them as Handlebars.js does. Keyword lists are lists, not objects.
  • false, nil, "", 0 and [] are false in #if and #unless, where includeZero=true counts 0 as true. #with renders its {{else}} for all of them but 0.
  • Numbers and booleans are written as JavaScript writes them (1.0 is 1), lists as their elements joined with commas, and maps and objects as [object Object]. Structs that implement String.Chars, such as Date, are written with it.

Escaping

{{value}} escapes &, <, >, ", ', ` and =. {{{value}}}, {{&value}}, values made with Braces.Handlebars.safe/1 and {:safe, iodata} tuples are written as they are.

no_escape: true turns escaping off for the whole render: use it for text that isn't HTML, such as an email's subject or plain-text body.

Escaping makes values safe in HTML text and quoted attributes, not in URLs, <script> or style. See Security.

Braces.Handlebars.render_to_string(Braces.Handlebars.compile!("Welcome, {{name}}"), %{name: "Ada & Co"}, no_escape: true)
#=> {:ok, "Welcome, Ada & Co"}

Built-in helpers

The helpers are Handlebars.js's built-ins: if, unless, each, with, lookup and log (to Logger), with {{else}}, {{else if}}, block params ({{#each items as |item i|}}), @index, @key, @first, @last, @root and ../. A block named after a value renders as a Mustache section. Add your own with helpers: and partials: (see Helpers and partials).

Options

OptionEffect
strict: trueA missing value is an error, as Handlebars.js's strict option makes it.
no_escape: true{{value}} is written without escaping.
data: %{...}Adds @ variables, as Handlebars.js's data runtime option does.
helpers:Adds helpers: a module that uses Braces.Handlebars.Helpers, a map of names to functions, or a list of them.
partials:Adds partials: a map of names to templates from ~HBS or Braces.Handlebars.compile/2.

Errors

What can't be rendered is a Braces.Handlebars.RenderError, at the tag that failed, with the line and the same caret as a Braces.Handlebars.ParseError:

"name" not defined in data - 1:5

    Hi {{name}}
         ^

A helper that raises lets the exception through; render/3 doesn't rescue it.