Phlex.Helpers (phlex v0.3.0)

Copy Markdown View Source

Helper functions and macros for Phlex components.

This module provides convenience macros and functions to make component definition easier and more idiomatic.

Summary

Functions

Macro to define a component with a simpler API.

Macro to define an SVG component with a simpler API.

Escapes HTML entities in a string.

Extracts bindings from keyword list or map.

Merges attributes intelligently, handling arrays, sets, hashes, and strings.

Functions

defcomponent(name, list)

(macro)

Macro to define a component with a simpler API.

Example

defcomponent Card do
  def view_template(assigns, state) do
    state
    |> div([class: "card"], fn state ->
      Phlex.SGML.append_text(state, assigns.title)
    end)
  end
end

This is equivalent to:

defmodule Card do
  use Phlex.HTML

  def view_template(assigns, state) do
    # ...
  end
end

defsvg_component(name, list)

(macro)

Macro to define an SVG component with a simpler API.

escape_html(string)

Escapes HTML entities in a string.

grab(bindings)

Extracts bindings from keyword list or map.

Example

grab(foo: foo, bar: bar)
# => {foo, bar} if multiple, or single value if one

mix(attrs1, attrs2)

Merges attributes intelligently, handling arrays, sets, hashes, and strings.

Example

mix([class: "foo"], [class: "bar"])
# => [class: "foo bar"]

mix([class: ["a", "b"]], [class: ["c"]])
# => [class: ["a", "b", "c"]]