Exampple.Template (exampple v0.10.6)

Templates are XML chunks which could be in use for a Exampple.Client. The template is usually a string which could contain special execution code using an escape sintaxis %{var}.

The templates are stored in ETS tables for each node. It was not thought for the implementation as a cluster so it will be needed to start it and populate it individually for each node.

Link to this section Summary

Functions

Retrieve all of the templates as a list of tuples `{name(), }

Retrieve a template given the name of the template.

Template requires to be initiated to start working. It's a good idea to start it from an Application process because if the process who spawned dies, the ETS table also dies.

Adds several templates at once.

Adds a template to be in use by the process when we call send_template/2 or send_template/3. The name is the name or PID for the process, the name is the name we will use storing the template and xml is the text or function which will generate the stanza.

Use a template registered. This let us to trigger faster stanzas when we are working from the shell, developing tests or even when we have our code narrowed and focused on the business logic.

See render/1. It performs the same action but triggering an error if the template name wasn't found.

Link to this section Types

Specs

bindings() :: [{key(), content()}]

Specs

content() :: rendered() | (Keyword.t() -> rendered())

Specs

key() :: atom()

Specs

name() :: String.t()

Specs

rendered() :: String.t() | Exampple.Xml.Xmlel.t()

Link to this section Functions

Specs

all() :: [{name(), content()}]

Retrieve all of the templates as a list of tuples `{name(), }

Specs

get(name()) :: content() | nil

Retrieve a template given the name of the template.

Specs

init() :: :ok

Template requires to be initiated to start working. It's a good idea to start it from an Application process because if the process who spawned dies, the ETS table also dies.

Specs

put([{name(), content()}]) :: [{name(), content()}]

Adds several templates at once.

Specs

put(name(), content()) :: content()

Adds a template to be in use by the process when we call send_template/2 or send_template/3. The name is the name or PID for the process, the name is the name we will use storing the template and xml is the text or function which will generate the stanza.

Link to this function

render(name, bindings \\ [])

Specs

render(name(), bindings()) :: {:ok, rendered()} | {:error, :not_found}

Use a template registered. This let us to trigger faster stanzas when we are working from the shell, developing tests or even when we have our code narrowed and focused on the business logic.

The name parameter is the name to lookup the template. The args are the arguments passed to the function template.

Examples:

iex> :ok = Exampple.Template.init()
iex> Exampple.Template.put("gret", "Hello %{name}!")
iex> Exampple.Template.render("gret", name: "World")
{:ok, "Hello World!"}

iex> :ok = Exampple.Template.init()
iex> Exampple.Template.put("gret", &"Hello #{&1[:name]}!")
iex> Exampple.Template.render("gret", name: "World")
{:ok, "Hello World!"}
Link to this function

render!(name, bindings \\ [])

Specs

render!(name(), bindings()) :: rendered()

See render/1. It performs the same action but triggering an error if the template name wasn't found.

Examples:

iex> :ok = Exampple.Template.init()
iex> Exampple.Template.put("gret", "Hello %{name}!")
iex> Exampple.Template.render!("gret", name: "World")
"Hello World!"

iex> :ok = Exampple.Template.init()
iex> Exampple.Template.put("gret", &"Hello #{&1[:name]}!")
iex> Exampple.Template.render!("gret", name: "World")
"Hello World!"

iex> :ok = Exampple.Template.init()
iex> Exampple.Template.render!("missing", name: "World")
** (ArgumentError) not_found