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"})
In practice though, 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↑
engines() | Returns a keyword list with all template engines extensions followed by their modules |
find_all(root) | Returns all template paths in a given template root |
format_encoder(template_name) | Returns the format encoder for the given template name |
hash(root) | Returns the hash of all template paths in the given root |
module_to_template_root(module, base, suffix) | Converts a module, without the suffix, to a template root |
template_path_to_name(path, root) | Converts the template path into the template name |
Types ↑
name :: binary
path :: binary
root :: binary
Functions
Specs:
- engines :: %{atom => module}
Returns a keyword list with all template engines extensions followed by their modules.
Specs:
Returns all template paths in a given template root.
Specs:
- format_encoder(name) :: module | nil
Returns the format encoder for the given template name.
Specs:
- hash(root) :: binary
Returns the hash of all template paths in the given root.
Used by Phoenix to check if a given root path requires recompilation.
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")
""
Specs:
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"