View Source Hypa.Plug (Hypa v0.1.0)

A Plug offering functions useful for applications using HTMX.

If you are using Phoenix, add this Plug to your browser pipeline:

pipeline :browser do
  plug Hypa.Plug
end

Summary

Functions

Callback implementation for Plug.call/2.

Extracts HTMX headers into the conn's assigns.

Callback implementation for Plug.init/1.

Functions

Callback implementation for Plug.call/2.

Link to this function

detect_htmx_request(conn, opts)

View Source

Extracts HTMX headers into the conn's assigns.

See Request Headers Reference for documentation on HTMX's header values.

Examples

iex> conn(:get, "/hello")
...> |> merge_req_headers([
...>      {"hx-request", "true"},
...>      {"hx-current-url", "/current"},
...>      {"hx-history-restore-request", "false"},
...>      {"hx-target", "my-target-element-id"},
...>      {"hx-trigger-name", "my-triggered-element-name"},
...>      {"hx-trigger", "my-triggered-element-id"}
...>    ])
...> |> Hypa.Plug.detect_htmx_request(Hypa.Plug.init([]))
...> |> Map.get(:assigns)
...> |> Map.get(:htmx)
%{
  request: true,
  boosted: false,
  current_url: "/current",
  history_restore_request: false,
  prompt: nil,
  target: "my-target-element-id",
  trigger: "my-triggered-element-id",
  trigger_name: "my-triggered-element-name"
}

If the request does not have HTMX headers, the :htmx assign will not be present.

iex> conn(:get, "/htmxless")
...> |> Hypa.Plug.detect_htmx_request(Hypa.Plug.init([]))
...> |> Map.get(:assigns)
...> |> Map.has_key?(:htmx)
false

Callback implementation for Plug.init/1.