FrancisTemplate.Engine behaviour (Francis Template v0.2.0)

View Source

Behaviour for pluggable template engines.

An engine receives the path to a template file and the assigns to render it with, and returns the rendered output as iodata (a binary is valid iodata).

The default engine is FrancisTemplate.EEx, registered for the .eex extension. Engines are resolved per-template by file extension, so multiple engines can coexist in the same application.

To use a different engine, implement this behaviour and register it for an extension:

defmodule MyApp.UpcaseEngine do
  @behaviour FrancisTemplate.Engine

  @impl true
  def render(path, _assigns), do: path |> File.read!() |> String.upcase()
end

# config/config.exs
config :francis_template, engines: %{"up" => MyApp.UpcaseEngine}

Note that engines differ in how they interpret assigns: FrancisTemplate.EEx expects atom-keyed assigns exposed through the @ syntax, while other engines might expect string-keyed data. Normalising the assigns is the engine's concern.

Summary

Callbacks

render(path, assigns)

@callback render(path :: String.t(), assigns :: map() | keyword()) :: iodata()