Mustache templates, rendered as the Mustache spec (1.4) says.
iex> {:ok, template} = Braces.Mustache.compile("Hi {{name}}! {{#items}}<{{.}}>{{/items}}")
iex> Braces.Mustache.render_to_string(template, %{"name" => "Ada & co", "items" => ["a", "b"]})
{:ok, "Hi Ada & co! <a><b>"}The ~MUSTACHE sigil parses a template when your code compiles (import Braces.Mustache, only: :sigils), and Braces.Mustache.Phoenix.Engine renders .mustache files in Phoenix.
What renders
- A name is looked up in every enclosing context. In
{{#user}}{{site}}{{/user}},siteis read fromuser, and then from the data around it. Once the first part of a dotted name is found,{{a.b}}, the rest is read from there. - Sections repeat for a list, render once with their value as the context for any other
value that's true, and not at all for
nil,false,[]or a missing value. Inverted sections,{{^name}}, render when it's one of those. - Delimiters can change:
{{=<% %>=}}makes the tags<% name %>, until they change again. - Partials,
{{> name}}, frompartials:.
A tag alone on its line, a section, inverted section, closing tag, comment, partial or delimiter change, takes its line with it, and a partial alone on its line is indented as the tag is.
Values are HTML-escaped in {{name}}, and written as they are in {{{name}}} and
{{&name}}. Maps are read by string keys, then by atom keys, so data from JSON, structs and
maps with atom keys all work.
Lambdas, dynamic partial names and template inheritance, the spec's optional parts, aren't supported: a function in the data is a value like any other.
Summary
Functions
Parses a Mustache template, giving a Braces.Mustache.Template or a
Braces.Mustache.ParseError.
Like compile/1, raising Braces.Mustache.ParseError when the template doesn't parse.
Renders a template, or Mustache source, with data, to iodata.
Like render/3, giving a string.
The ~MUSTACHE sigil: a Mustache template parsed when your code compiles, so a template
that doesn't parse is a CompileError at its line. Import it with
import Braces.Mustache, only: :sigils.
Functions
Parses a Mustache template, giving a Braces.Mustache.Template or a
Braces.Mustache.ParseError.
Like compile/1, raising Braces.Mustache.ParseError when the template doesn't parse.
Renders a template, or Mustache source, with data, to iodata.
partials:a map of names to Mustache source or templates, for{{> name}}. A partial that isn't there renders nothing, as the spec says.escape: falsewrites values without HTML escaping, for text that isn't HTML.
Like render/3, giving a string.
The ~MUSTACHE sigil: a Mustache template parsed when your code compiles, so a template
that doesn't parse is a CompileError at its line. Import it with
import Braces.Mustache, only: :sigils.
iex> import Braces.Mustache, only: :sigils
iex> Braces.Mustache.render_to_string(~MUSTACHE"Hi {{name}}!", %{"name" => "Ada"})
{:ok, "Hi Ada!"}