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>"})
"&lt;b&gt;"

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 "" (default false)
  • :engine — output engine module (default Jennie.Engine)
  • :ignore_nil — leave tags whose keys are absent in place (default false)

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

compile(source, opts \\ [])

@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{}}.

compile!(source, opts \\ [])

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

Like compile/2 but raises Jennie.SyntaxError on failure.

missing?(source, data)

@spec missing?(String.t(), term()) :: [String.t()]

Names referenced by source whose top-level key is absent from data.

Examples

iex> Jennie.missing?("{{a}}{{b}}", %{"a" => 1})
["b"]

render(source_or_template, data \\ %{}, opts \\ [])

@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)"

scan(source)

@spec scan(String.t()) :: [String.t()]

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"]