expug v0.9.2 Expug View Source

Expug compiles templates to an eex template.

to_eex/2 turns an Expug source into an EEx template.

iex> source = "div\n  | Hello"
iex> Expug.to_eex(source)
{:ok, "<div>\nHello<%= \"\\n\" %></div>\n"}

to_eex!/2 is the same, and instead returns the result or throws an Expug.Error.

iex> source = "div\n  | Hello"
iex> Expug.to_eex!(source)
"<div>\nHello<%= \"\\n\" %></div>\n"

Errors

to_eex/2 will give you this in case of an error:

{:error, %{
  type: :parse_error,
  position: {3, 2},    # line/col
  ...                  # other metadata
}}

Internally, the other classes will throw %{type, position, ...} which will be caught here.

The raw helper

Note that it needs raw/1, something typically provided by Phoenix.HTML. You don’t need Phoenix.HTML however; a binding with raw/1 would do.

iex> Expug.to_eex!(~s[div(role="alert")= @message])
"<div<%= raw(Expug.Runtime.attr(\"role\", \"alert\")) %>><%= \"\\n\" %><%= @message %><%= \"\\n\" %></div>\n"

Internal notes

Expug.to_eex/2 pieces together 4 steps into a pipeline:

Also see

Link to this section Summary

Link to this section Functions

Link to this function to_eex(source, opts \\ []) View Source

Compiles an Expug template to an EEx template.

Returns {:ok, result}, where result is an EEx string. On error, it will return {:error, ...}.

Options

All options are optional.

  • attr_helper (String) - the attribute helper to use (default: "Expug.Runtime.attr")
  • raw_helper (String) - the raw helper to use (default: "raw")
Link to this function to_eex!(source, opts \\ []) View Source

Compiles an Expug template to an EEx template and raises errors on failure.

Returns the EEx string on success. On failure, it raises Expug.Error.