Jennie (jennie v0.9.0)
Jennie — logic-less templates inspired by Moustache.
Quick start
iex> Jennie.render("Hello {{name}}!", %{"name" => "World"})
"Hello World!"
iex> Jennie.render("{{x}}", %{"x" => "<b>"})
"<b>"
iex> Jennie.render("{{{x}}}", %{"x" => "<b>"})
"<b>"Compile once, render many
{:ok, tpl} = Jennie.compile("Hi {{name}}")
Jennie.render(tpl, %{"name" => "A"})
Jennie.render(tpl, %{"name" => "B"})Options
:escape— a(binary -> iodata)escaper (default: HTML):partials— a%{name => source}map or(name -> source | nil)fun:raise_on_missing_partial— raise instead of rendering""(defaultfalse):engine— output engine module (defaultJennie.Engine):ignore_nil— leave tags whose keys are absent in place (defaultfalse)
Summary
Functions
Compile source into a reusable Jennie.Template.
Like compile/2 but raises Jennie.SyntaxError on failure.
Names referenced by source whose top-level key is absent from data.
Render a template against data.
List the names of every tag in source that references the data — variables,
unescaped tags, sections, and inverted sections. Comments, partials, and
delimiter tags are excluded, as is the implicit iterator {{.}}. Names are
deduplicated while preserving first-seen order.
Functions
@spec compile( String.t(), keyword() ) :: {:ok, Jennie.Template.t()} | {:error, Jennie.SyntaxError.t()}
Compile source into a reusable Jennie.Template.
Returns {:ok, template} or {:error, %Jennie.SyntaxError{}}.
@spec compile!( String.t(), keyword() ) :: Jennie.Template.t()
Like compile/2 but raises Jennie.SyntaxError on failure.
Names referenced by source whose top-level key is absent from data.
Examples
iex> Jennie.missing?("{{a}}{{b}}", %{"a" => 1})
["b"]
@spec render(String.t() | Jennie.Template.t(), term(), keyword()) :: binary()
Render a template against data.
source_or_template may be a raw string or a pre-compiled Jennie.Template.
Non-map data (a scalar, list, or struct) is rendered as the top-level
implicit-iterator context, reachable via {{.}}.
Examples
iex> Jennie.render("{{.}} miles", 85)
"85 miles"
iex> Jennie.render("{{#.}}({{value}}){{/.}}", [%{"value" => "a"}, %{"value" => "b"}])
"(a)(b)"
List the names of every tag in source that references the data — variables,
unescaped tags, sections, and inverted sections. Comments, partials, and
delimiter tags are excluded, as is the implicit iterator {{.}}. Names are
deduplicated while preserving first-seen order.
Examples
iex> Jennie.scan("{{a}}{{#b}}{{c}}{{/b}}")
["a", "b", "c"]