Phoenix v1.2.4 Phoenix.Template

Templates are used by Phoenix on rendering.

Since many views require rendering large contents, for example a whole HTML file, it is common to put those files in the file system into a particular directory, typically “web/templates”.

This module provides conveniences for reading all files from a particular directory and embedding them into a single module. Imagine you have a directory with templates:

# templates/foo.html.eex
Hello <%= @name %>

# templates.ex
defmodule Templates do
  use Phoenix.Template, root: "templates"
end

Now the template foo can be directly rendered with:

Templates.render("foo.html", %{name: "John Doe"})

Options

  • :root - the root template path to find templates
  • :pattern - the wildcard pattern to apply to the root when finding templates. Default "*"

Rendering

In some cases, you will want to overide the render/2 clause to compose the assigns for the template before rendering. In such cases, you can render the template directly by calling the generated private function render_template/2. For example:

# templates/foo.html.eex
Hello <%= @name %>

# templates.ex
defmodule Templates do
  use Phoenix.Template, root: "templates"

  def render("foo.html", %{name: name}) do
    render_template("foo.html", %{name: String.upcase(name)})
  end
end

In practice, developers rarely use Phoenix.Template directly. Instead they use Phoenix.View which wraps the template functionality and adds some extra conveniences.

Terminology

Here is a quick introduction into Phoenix templates terms:

  • template name - is the name of the template as given by the user, without the template engine extension, for example: “users.html”

  • template path - is the complete path of the template in the filesystem, for example, “path/to/users.html.eex”

  • template root - the directory were templates are defined

  • template engine - a module that receives a template path and transforms its source code into Elixir quoted expressions.

Custom Template Engines

Phoenix supports custom template engines. Engines tell Phoenix how to convert a template path into quoted expressions. Please check 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

Besides template engines, Phoenix has the concept of format encoders. Format encoders work per format and are responsible for encoding a given format to string once the view layer finishes processing.

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, :format_encoders,
  html: Phoenix.HTML.Engine,
  json: Poison

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 name

Returns the hash of all template paths in the given root

Converts a module, without the suffix, to a template root

Converts the template path into the template name

Types

name :: binary
path :: binary
root :: binary

Functions

engines()

Specs

engines :: %{optional(atom) => module}

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

find_all(root, pattern \\ "*")

Specs

find_all(root, pattern :: String.t) :: [path]

Returns all template paths in a given template root.

format_encoder(template_name)

Specs

format_encoder(name) :: module | nil

Returns the format encoder for the given template name.

hash(root, pattern \\ "*")

Specs

hash(root, pattern :: String.t) :: binary

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

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

module_to_template_root(module, base, suffix)

Converts a module, without the suffix, to a template root.

Examples

iex> Phoenix.Template.module_to_template_root(MyApp.UserView, MyApp, "View")
"user"

iex> Phoenix.Template.module_to_template_root(MyApp.Admin.User, MyApp, "View")
"admin/user"

iex> Phoenix.Template.module_to_template_root(MyApp.Admin.User, MyApp.Admin, "View")
"user"

iex> Phoenix.Template.module_to_template_root(MyApp.View, MyApp, "View")
""

iex> Phoenix.Template.module_to_template_root(MyApp.View, MyApp.View, "View")
""
template_path_to_name(path, root)

Specs

template_path_to_name(path, root) :: name

Converts the template path into the template name.

Examples

iex> Phoenix.Template.template_path_to_name(
...>   "lib/templates/admin/users/show.html.eex",
...>   "lib/templates")
"admin/users/show.html"