Hyperex v0.2.0 Hyperex View Source
A pure-Elixir HTML renderer.
Link to this section Summary
Functions
Generates renderable elements
A helper that prepends an HTML5 doctype
This function should not be directly used. It has to be public because the
h
macro inserts calls to merge_props
Creates HTML iodata from elements
Link to this section Types
element()
View Source
element() :: unescaped_element() | regular_element()
element() :: unescaped_element() | regular_element()
regular_element()
View Source
regular_element() ::
{tag(),
%{optional(:children) => :void | renderable(), optional(any()) => any()}}
regular_element() :: {tag(), %{optional(:children) => :void | renderable(), optional(any()) => any()}}
renderable() View Source
tag()
View Source
tag() :: atom()
tag() :: atom()
unescaped_element()
View Source
unescaped_element() ::
{:dangerously_unescaped, binary(), renderable(), binary()}
unescaped_element() :: {:dangerously_unescaped, binary(), renderable(), binary()}
Link to this section Functions
h(a) View Source (macro)
Generates renderable elements.
The first parameter should be the tag name or a function name. Tag names can be atoms or strings. If the first parameter is a function, then it should return renderable elements.
The next parameters are what is called “props” in the React world. Each of
these parameters must be a keyword list or a map. These maps are merged
during rendering (values in the rightmost ones override values in the
leftmost ones, see Map.merge/2
).
If the last parameter is not a map or a keyword list, then it is used as
the children
prop. So h :div, "foo"
is equivalent to
h :div, [children: "foo"]
.
Children can be rendered with a children
prop or an optional
do … end
block.
Use render/1
to convert the returned renderable elements into
iodata or strings.
Example
iex> import Hyperex
iex> require Hyperex
iex> h :html do
...> h :h1 do "Hello" end
...> end
{"html", %{children: {"h1", %{children: "Hello"}}}}
h(a, b) View Source (macro)
h(a, b, c) View Source (macro)
h(a, b, c, d) View Source (macro)
h(a, b, c, d, e) View Source (macro)
h(a, b, c, d, e, f) View Source (macro)
h(a, b, c, d, e, f, g) View Source (macro)
h(a, b, c, d, e, f, g, h) View Source (macro)
html5_doctype(map) View Source
A helper that prepends an HTML5 doctype.
Example
iex> import Hyperex
iex> require Hyperex
iex> to_string render(
...> h html5_doctype do
...> h :html do
...> h :body do
...> "hello"
...> end
...> end
...> end
...> )
"<!DOCTYPE html><html ><body >hello</body></html>"
merge_props(list) View Source
This function should not be directly used. It has to be public because the
h
macro inserts calls to merge_props
.
render(renderable)
View Source
render(renderable()) :: iodata()
render(renderable()) :: iodata()
Creates HTML iodata from elements.
Example
iex> import Hyperex
iex> require Hyperex
iex> renderable = h :html do h :h1 do "Hello" end end
{"html", %{children: {"h1", %{children: "Hello"}}}}
iex> render(renderable)
[
60,
"html",
32,
[""],
62,
[60, "h1", 32, [""], 62, "Hello", "</", "h1", 62],
"</",
"html",
62
]
iex> to_string(render(renderable))
"<html ><h1 >Hello</h1></html>"