Braces.Mustachio (Braces v0.1.0)

Copy Markdown View Source

Mustachio templates, the language of Postmark's templates, rendered as Mustachio renders them.

iex> Braces.Mustachio.render_to_string("Hi {{ name }}! {{#company}}{{ name }}{{^company}}No company{{/company}}", %{"name" => "Ada", "company" => %{"name" => "Acme"}})
{:ok, "Hi Ada! Acme"}

Rendering

render/3 renders a template with its data. compile/2 compiles one to render many times, and the ~MUSTACHIO sigil compiles it with your code, so a template that can't be rendered is a CompileError at its line:

import Braces.Mustachio, only: :sigils
Braces.render(~MUSTACHIO"Hi {{ name }}", data)

Braces.Mustachio.Phoenix.Engine renders .mustachio files in Phoenix. Braces.convert/2 converts Mustachio to the other languages.

What renders

  • {{ name }}, {{{ name }}}, and paths, {{ company.name }}.
  • {{#company}}…{{/company}} renders its block once, with company as the context, when it's there, even for a list; {{^company}} in the block is its else.
  • {{#each list}} repeats its block for each item.
  • ../ goes up one segment of the path: in {{#each company.employees}}, {{../../name}} is the company's name.
  • A block tag alone on its line keeps the line.
  • {{{ @content }}}, in a layout, is the template's HTML: render the layout with data: %{"content" => Braces.Handlebars.safe(html)}.
  • {{{ pm:unsubscribe }}} is the value pm:unsubscribe, so give it in the data.

Braces differs from Mustachio in three ways: it writes false as false where Mustachio writes False, it escapes ` and =, and an empty map is true.

A template Mustachio would reject, with an invalid path, or a block that isn't closed, is a Braces.ConversionError.

Summary

Rendering

Compiles Mustachio into a Braces.Template for render/3, or gives the error that convert/2 gives.

Like compile/2, raising the error.

Renders Mustachio, or a Braces.Template of it, with data, to iodata.

The ~MUSTACHIO sigil: Mustachio compiled with your code into a Braces.Template, so a template that doesn't convert is a CompileError at its line. Import it with import Braces.Mustachio, only: :sigils.

Functions

Converts a Mustachio template to Handlebars source, or gives a Braces.ConversionError with the tag's line and column.

Like convert/2, raising Braces.ConversionError when the template can't be converted.

Reads a Mustachio template into a Braces.IR, for Braces.convert/2.

Rendering

compile(source, opts \\ [])

@spec compile(String.t(), keyword()) ::
  {:ok, Braces.Template.t()} | {:error, Exception.t()}

Compiles Mustachio into a Braces.Template for render/3, or gives the error that convert/2 gives.

  • escape: false writes values without HTML escaping.
  • templates: the templates it extends or includes, by name: a map of names to Mustachio source or Braces.Templates, or a function of a name giving {:ok, template} or :error. They're compiled into it.

compile!(source, opts \\ [])

@spec compile!(String.t(), keyword()) :: Braces.Template.t()

Like compile/2, raising the error.

render(template, data, opts \\ [])

@spec render(String.t() | Braces.Template.t(), term(), keyword()) ::
  {:ok, iodata()} | {:error, Exception.t()}

Renders Mustachio, or a Braces.Template of it, with data, to iodata.

Takes helpers:, functions for filters Braces doesn't have, as modules that use Braces.Handlebars.Helpers, and, with source, escape: and templates:, as compile/2 does.

render_to_string(template, data, opts \\ [])

@spec render_to_string(String.t() | Braces.Template.t(), term(), keyword()) ::
  {:ok, String.t()} | {:error, Exception.t()}

Like render/3, giving a string.

sigil_MUSTACHIO(arg, modifiers)

(macro)

The ~MUSTACHIO sigil: Mustachio compiled with your code into a Braces.Template, so a template that doesn't convert is a CompileError at its line. Import it with import Braces.Mustachio, only: :sigils.

Functions

convert(source, opts \\ [])

Converts a Mustachio template to Handlebars source, or gives a Braces.ConversionError with the tag's line and column.

convert!(source, opts \\ [])

Like convert/2, raising Braces.ConversionError when the template can't be converted.

to_ir(source, opts \\ [])

Reads a Mustachio template into a Braces.IR, for Braces.convert/2.