View Source Phoenix.Template (phoenix_template v0.1.0)

Templates are markup languages that are compiled to Elixir code.

This module provides functions for loading and compiling templates from disk. A markup language is compiled to Elixir code via an engine. See Phoenix.Template.Engine.

In practice, developers rarely use Phoenix.Template directly. Instead, libraries such as Phoenix.View and Phoenix.LiveView use it as a building block.

custom-template-engines

Custom Template Engines

Phoenix supports custom template engines. Engines tell Phoenix how to convert a template path into quoted expressions. See Phoenix.Template.Engine for more information on the API required to be implemented by custom engines.

Once a template engine is defined, you can tell Phoenix about it via the template engines option:

config :phoenix, :template_engines,
  eex: Phoenix.Template.EExEngine,
  exs: Phoenix.Template.ExsEngine

format-encoders

Format encoders

Besides template engines, Phoenix has the concept of format encoders. Format encoders work per format and are responsible for encoding a given format to a string. For example, when rendering JSON, your templates may return a regular Elixir map. Then the JSON format encoder is invoked to convert it to JSON.

A format encoder must export a function called encode_to_iodata!/1 which receives the rendering artifact and returns iodata.

New encoders can be added via the format encoder option:

config :phoenix_template, :format_encoders,
  html: Phoenix.HTML.Engine

Link to this section Summary

Functions

Returns a keyword list with all template engines extensions followed by their modules.

Returns all template paths in a given template root.

Returns the format encoder for the given template.

Returns the hash of all template paths in the given root.

Link to this section Types

Link to this section Functions

Link to this macro

compile_all(converter, root, pattern \\ "*", engines \\ nil)

View Source (macro)
@spec engines() :: %{required(atom()) => module()}

Returns a keyword list with all template engines extensions followed by their modules.

Link to this function

find_all(root, pattern \\ "*", engines \\ engines())

View Source
@spec find_all(root(), pattern :: String.t(), %{required(atom()) => module()}) :: [
  path()
]

Returns all template paths in a given template root.

@spec format_encoder(path()) :: module() | nil

Returns the format encoder for the given template.

Link to this function

hash(root, pattern \\ "*", engines \\ engines())

View Source
@spec hash(root(), pattern :: String.t(), %{required(atom()) => module()}) :: binary()

Returns the hash of all template paths in the given root.

Used by Phoenix to check if a given root path requires recompilation.

Link to this function

module_to_template_root(module, base, suffix)

View Source
This function is deprecated. Use Phoenix.View.module_to_template_root/3.
Link to this function

template_path_to_name(path, root)

View Source
This function is deprecated. Use Phoenix.View.template_path_to_name/3.