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

Link to this type

regular_element() View Source
regular_element() ::
  {tag(),
   %{optional(:children) => :void | renderable(), optional(any()) => any()}}

Link to this type

renderable() View Source
renderable() :: [element()] | element() | binary() | number() | nil

Link to this type

unescaped_element() View Source
unescaped_element() ::
  {:dangerously_unescaped, binary(), renderable(), binary()}

Link to this section Functions

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"}}}}
Link to this macro

h(a, b, c, d, e) View Source (macro)

Link to this macro

h(a, b, c, d, e, f) View Source (macro)

Link to this macro

h(a, b, c, d, e, f, g) View Source (macro)

Link to this macro

h(a, b, c, d, e, f, g, h) View Source (macro)

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>"

This function should not be directly used. It has to be public because the h macro inserts calls to merge_props.

Link to this function

render(renderable) View Source
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>"