Mau (mau v0.7.1)

View Source

Mau template engine for the Prana template language.

Provides three main functions:

  • compile/2 - Parse template string into AST
  • render/3 - Render template string or AST with context
  • render_map/3 - Recursively render template strings in nested maps with directive support

Map Directives

The render_map/3 function supports powerful map transformation directives:

  • #map - Iterate over collections and apply templates to each item with index and parent access
  • #merge - Combine multiple maps together
  • #if - Conditional rendering based on boolean conditions
  • #filter - Filter collections based on conditions with index access
  • #pick - Extract specific keys from maps

See docs/map_directives_reference.md for comprehensive documentation.

Summary

Functions

Compiles a template string into an AST.

Renders a template string or AST with the given context.

Recursively renders template strings in nested maps.

Functions

compile(template, opts \\ [])

Compiles a template string into an AST.

Handles text and expression blocks.

Options

  • :strict_mode - boolean, default false
  • :max_template_size - integer, maximum template size in bytes (no limit by default)

Examples

iex> Mau.compile("Hello world")
{:ok, [{:text, ["Hello world"], []}]}

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

Renders a template string or AST with the given context.

Options

  • :preserve_types - boolean, default false. When true, preserves data types for single-value templates (templates that render to a single expression result).
  • :max_template_size - integer, maximum template size in bytes (no limit by default)
  • :max_loop_iterations - integer, maximum loop iterations (default 10000)

Examples

iex> Mau.render("Hello world", %{})
{:ok, "Hello world"}

# Type preservation for single values
iex> Mau.render("{{ 42 }}", %{}, preserve_types: true)
{:ok, 42}

iex> Mau.render("{{ user.active }}", %{"user" => %{"active" => true}}, preserve_types: true)
{:ok, true}

# Mixed content always returns strings
iex> Mau.render("Count: {{ items | length }}", %{"items" => [1,2,3]}, preserve_types: true)
{:ok, "Count: 3"}

render_map(nested_map, context, opts \\ [])

Recursively renders template strings in nested maps.

For Group 1, only handles plain text templates.

Examples

iex> Mau.render_map(%{message: "Hello world"}, %{})
{:ok, %{message: "Hello world"}}