HTMX utilities for Elixir https://hex.pm/packages/hypa
Find a file
renovate-bot f2e128d30a
All checks were successful
renovate/stability-days Updates have met minimum release age requirement
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
chore(deps): update dependency erlang to v29.0.1
2026-06-04 00:02:10 +00:00
.woodpecker chore(deps): Update Elixir to 1.20 2026-06-03 14:17:19 -06:00
lib feat: add specs, version numbers to docs, update links 2026-06-03 12:11:29 -06:00
test feat: add multiple template rendering 2025-09-22 22:47:27 -06:00
.check.exs fix: fix check warnings 2025-09-23 13:50:59 -06:00
.formatter.exs Use ex_check, fix warnings 2025-08-06 10:14:46 -06:00
.gitignore Rename to Hypa, prepare for release 2024-07-18 11:08:33 -06:00
CHANGELOG.md fix(docs): Use Elixir highlighting for code block 2026-06-03 12:18:23 -06:00
LICENSE.md Rename to Hypa, prepare for release 2024-07-18 11:08:33 -06:00
mise.toml chore(deps): update dependency erlang to v29.0.1 2026-06-04 00:02:10 +00:00
mix.exs chore(deps): Update Elixir to 1.20 2026-06-03 14:17:19 -06:00
mix.lock chore(deps): update dependency ex_doc to v0.40.3 2026-05-29 00:03:22 +00:00
README.md feat: add specs, version numbers to docs, update links 2026-06-03 12:11:29 -06:00
renovate.json Update renovate.json 2026-05-27 16:00:37 +00:00

Hypa

status-badge Hex.pm Version Hex.pm License hexdocs.pm

Useful functions for Elixir applications using htmx.

Offers a Plug that performs a few tasks:

  • Parses HX-* headers into conn.assigns
  • Adds htmx to your cache-control headers
  • Controls the rendering of Phoenix layouts, hiding them for htmx requests while showing them for non-htmx requests.

Installation

This library is available in Hex, and the package can be installed by adding hypa to your list of dependencies in mix.exs:

def deps do
  [
    {:hypa, "~> 0.2.0"}
  ]
end

Documentation can be found at https://hexdocs.pm/hypa.

Usage

Add Hypa.Plug to your Plug pipeline like this:

plug Hypa.Plug

Request headers

This plug adds htmx's request headers to conn.assigns:

header assign
HX-Boosted conn.assigns[:htmx][:boosted]
HX-Current-URL conn.assigns[:htmx][:current_url]
HX-History-Restore-Request conn.assigns[:htmx][:history_restore_request]
HX-Prompt conn.assigns[:htmx][:prompt]
HX-Request conn.assigns[:htmx][:request]
HX-Target conn.assigns[:htmx][:target]
HX-Trigger-Name conn.assigns[:htmx][:trigger_name]
HX-Trigger conn.assigns[:htmx][:trigger]

If the request does not have htmx headers, then conn.assigns[:htmx] will not be present.

Response headers

This plug also adds a Vary response header to your conn containing HX-Request, because your server is likely rendering different content for htmx requests. This value is prepended to the values already in the Vary header, so it will not overwrite any values you've already put in there. If you want to add more values to the Vary header yourself, be sure to use Plug.Conn.prepend_resp_headers/2 unless you do intend to overwite the entire Vary value.

header value
vary hx-request

The Hypa.Conn module also contains functions for adding htmx response headers to your conn.

Header Function
HX-Location Hypa.Conn.put_hx_location/2
HX-Push-Url Hypa.Conn.put_hx_push_url/2
HX-Redirect Hypa.Conn.put_hx_redirect/2
HX-Refresh Hypa.Conn.put_hx_refresh/2
HX-Replace-Url Hypa.Conn.put_hx_replace_url/2
HX-Reswap Hypa.Conn.put_hx_reswap/2
HX-Retarget Hypa.Conn.put_hx_retarget/2
HX-Reselect Hypa.Conn.put_hx_reselect/2
HX-Trigger Hypa.Conn.put_hx_trigger/2
HX-Trigger-After-Settle Hypa.Conn.put_hx_trigger_after_settle/2
HX-Trigger-After-Swap Hypa.Conn.put_hx_trigger_after_swap/2

Phoenix layouts

If your project is using Phoenix, Hypa.Plug will also disable Phoenix's root and app layouts when responding to htmx requests. If the htmx request is boosted or is a history restore request, however, the app layout will be enabled, since those requests expect an entire HTML body. The root layout is disabled for all htmx requests.

condition root layout app layout
HX-Request != "true" enabled enabled
HX-Boosted == "true" disabled enabled
HX-History-Restore-Request == "true" disabled enabled
HX-Request == "true" disabled disabled

Rendering multiple templates at once

If your project is using Phoenix, the Hypa.Phoenix module is available. It has the function render_many/2 which renders multiple templates and concatenates the result. This is useful for out-of-band swaps. Provide a list containing either template names, or tuples of template names and the assigns to give them.

conn
|> Hypa.Phoenix.render_many([
  :first,
  {:second, [a: 1]},
  {"third", [b: 2]}
])

License

Copyright © 2026 Rosa Richter

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.