Eml.Data protocol
The Eml Data protocol.
This protocol is used by Eml.to_content/3
function to
convert different Elixir data types to Eml content.
You can easily implement a protocol implementation
for a custom type, by defining a to_eml
function
that receives the custom type and outputs to eml
.
Example
iex> defmodule Customer do
...> defstruct [:name, :email, :phone]
...> end
iex> defimpl Eml.Data, for: Customer do
...> def to_eml(%Customer{name: name, email: email, phone: phone}) do
...> use Eml.Language.HTML
...>
...> div [class: "customer"] do
...> div [span("name: "), span(name)]
...> div [span("email: "), span(email)]
...> div [span("phone: "), span(phone)]
...> end
...> end
...> end
iex> div %Customer{name: "Fred", email: "freddy@mail.com", phone: "+31 6 5678 1234"}
#div<[#div<%{class: "customer"}
[#div<[#span<["name: "]>, #span<["Fred"]>]>,
#div<[#span<["email: "]>, #span<["freddy@mail.com"]>]>,
#div<[#span<["phone: "]>, #span<["+31 6 5678 1234"]>]>]>]>
Note that you can’t directly render a custom type, as the Eml.Data
protocol is not used during rendering.
If you want to directly render a custom type, convert it to eml
content first:
%Customer{name: "Fred", email: "freddy@mail.com", phone: "+31 6 5678 1234"} |> Eml.to_content |> Eml.render
# => "<div class='customer'><div><span>name: </span><span>Fred</span></div><div><span>email: </span><span>freddy@mail.com</span></div><div><span>phone: </span><span>+31 6 5678 1234</span></div></div>"
Summary↑
to_eml(data) |
Types ↑
t :: term
Functions
Specs:
- to_eml(Eml.Data.t) :: Eml.t