Helpers and partials

Copy Markdown View Source

This guide covers helpers and partials passed to Braces.Handlebars.render/3. Templates compiled with your module take theirs from use Braces.Handlebars instead (see Compiled templates).

Helpers

A helper is an Elixir function that takes the template's arguments, then a Braces.Handlebars.Helpers.Options, as a Handlebars.js helper takes its arguments and options. Write them as the public functions of a module that uses Braces.Handlebars.Helpers, or pass anonymous functions in a map:

defmodule MyApp.EmailHelpers do
  use Braces.Handlebars.Helpers

  # {{shout first_name}}
  def shout(text, _options), do: String.upcase(text)

  # {{#link url}}Your account{{/link}}
  def link(url, options) do
    Braces.Handlebars.safe(~s(<a href="#{Braces.Handlebars.escape(url)}">#{options.fn.(options.this)}</a>))
  end

  # {{money amount currency="EUR"}}
  def money(amount, options), do: "#{options.hash["currency"] || "USD"} #{amount}"
end

Braces.Handlebars.render(template, data, helpers: MyApp.EmailHelpers)
Braces.Handlebars.render(template, data, helpers: %{"shout" => fn text, _options -> String.upcase(text) end})
Braces.Handlebars.render(template, data, helpers: [MyApp.EmailHelpers, %{"shout" => &my_shout/2}])
  • A helper called with two arguments is called with three: the two and the options. A module can define a helper for several numbers of arguments; a helper called with a number it doesn't take is a Braces.Handlebars.RenderError.
  • options has name, hash (the key=value arguments, with string keys), data (the @ variables, with string keys), this (the current context), and, in a block helper, fn and inverse, which render the block and its {{else}} with a context and give text. Braces.Handlebars.Helpers.render_block/3 also takes data: to add @ variables and block_params: for as |a b|.
  • What a helper returns is a value like any other: escaped in {{helper}} unless it's made with Braces.Handlebars.safe/1, and written as it is in {{{helper}}} and by block helpers. Return a string: a list is written as JavaScript writes arrays. Braces.Handlebars.escape/1 escapes text for the HTML a helper builds.
  • Subexpressions pass a helper's value to another: {{shout (lookup user "name")}}, and {{link url label=(t "Home")}}.
  • A helper takes the place of a built-in helper or a value of the same name; a block param of the same name takes the place of the helper.
  • helperMissing is called for a helper that isn't defined, with its arguments and the name in options.name, and for a missing value, as in Handlebars.js. blockHelperMissing is called for a block named after a value, with the value. Without them, a missing helper with arguments is a Braces.Handlebars.RenderError (Missing helper: "shout") and a missing value is empty.
  • A helper that raises lets the exception through; render/3 doesn't rescue it.

Partials

Pass partials as a map of names to templates. Compile each one, so a partial that doesn't parse is caught where you define it, not when some render reaches it:

defmodule MyApp.Partials do
  import Braces.Handlebars.Sigil

  def partials do
    %{
      # Parsed and checked when this module compiles
      "header" => ~HBS"<h1>{{title}}</h1>",
      "layout" => ~HBS"<main>{{> @partial-block}}</main>",
      # Calls a helper given at render time, so compiled when it's loaded
      "footer" => Braces.Handlebars.compile!("<p>{{shout company}}</p>")
    }
  end
end

Braces.Handlebars.render(template, data, partials: MyApp.Partials.partials(), helpers: MyApp.EmailHelpers)
  • ~HBS (Braces.Handlebars.Sigil) parses the partial when your module compiles, and fails the build with a CompileError at its line. It's checked against the built-in helpers only, so it can't call a helper you pass to render/3: {{shout company}} would be Missing helper: "shout". A partial that calls your helpers is compiled with Braces.Handlebars.compile!/2, which raises a Braces.Handlebars.ParseError when it doesn't parse.
  • A partial you load at runtime, such as from your database, is compiled with Braces.Handlebars.compile/2 when you load or save it, so an error is reported to whoever wrote it (see Validating on save).
  • render/3 also takes a partial as source text, but then it's only parsed when a render calls it, on every render, and its errors only show up then. Don't pass source.
  • Partials kept in files beside your module are best compiled with the module: see Compiled templates, where a template that calls a partial that doesn't exist fails the build too.

Partials render as in Handlebars.js:

  • {{> header}} renders with the current context, {{> header user}} with user, and {{> header title="Hi"}} with the hash's keys added to the context. A partial sees @ variables such as @index and @root, but not the block params or ../ contexts of the template that calls it.
  • {{> (whichPartial)}} takes the partial's name from a subexpression.
  • {{#> layout}}…{{/layout}} renders layout, where {{> @partial-block}} renders the block; without a layout partial, it renders the block.
  • {{#*inline "name"}}…{{/inline}} defines a partial for the block it's in, and the partials it calls. Defined in a partial block, it's a partial of the partial: the usual layout pattern, {{#> layout}}{{#*inline "content"}}…{{/inline}}{{/layout}}.
  • A partial alone on its line indents each line it renders by the line's indentation.
  • An error inside a partial gives its line in the partial and the partial's name (Missing helper: "y" - 2:1 in partial footer).
  • A partial that isn't defined is a Braces.Handlebars.RenderError: The partial footer could not be found.