htmx

package
v1.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 10, 2026 License: Apache-2.0 Imports: 7 Imported by: 1

Documentation

Index

Constants

View Source
const (

	// HxBoosted indicates that the request is via an element using hx-boost
	HxBoosted = "HX-Boosted"

	// HxCurrentUrl the current URL of the browser
	HxCurrentUrl = "HX-Current-Url"

	// HxHistoryRestoreRequest “true” if the request is for history restoration after a miss in the local history cache
	HxHistoryRestoreRequest = "HX-History-Restore-Request"

	// HxPrompt the user response to an hx-prompt
	HxPrompt = "HX-Prompt"

	// HxRequest always “true” if it is htmx request
	HxRequest = "HX-Request"
	// HxTarget the id of the target element if it exists
	HxTarget = "HX-Target"

	// HxTriggerName the name of the triggered element if it exists
	HxTriggerName = "HX-Trigger-Name"

	// HxTrigger the id of the triggered element if it exists
	HxTrigger = "HX-Trigger"

	// HxLocation allows you to do a client-side redirect that does not do a full page reload
	HxLocation = "HX-Location"

	// HxPushUrl pushes a new url into the history stack
	HxPushUrl = "HX-Push-Url"

	// HxRedirect can be used to do a client-side redirect to a new location
	HxRedirect = "HX-Redirect"

	// HxRefresh if set to “true” the client-side will do a full refresh of the page
	HxRefresh = "HX-Refresh"

	// HxReplaceUrl replaces the current URL in the location bar
	HxReplaceUrl = "HX-Replace-Url"

	// HxReswap allows you to specify how the response will be swapped. See hx-swap for possible values
	HxReswap = "HX-Reswap"
	// HxRetarget a CSS selector that updates the target of the content update to a different element on the page
	HxRetarget = "HX-Retarget"
	// HxReselect a CSS selector that allows you to choose which part of the response is used to be swapped in. Overrides an existing hx-select on the triggering element
	HxReselect = "HX-Reselect"

	// HxTriggerAfterSettle allows you to trigger client-side events after the settle step
	HxTriggerAfterSettle = "HX-Trigger-After-Settle"
	// HxTriggerAfterSwap allows you to trigger client-side events after the swap step
	HxTriggerAfterSwap = "HX-Trigger-After-Swap"
)

Variables

View Source
var (
	Json = xun.Json
)

Functions

func CurrentURL added in v1.2.1

func CurrentURL(c *xun.Context) string

CurrentURL returns the current URL of the browser as reported by htmx via the "HX-Current-Url" header, or an empty string when the header is missing. This is the htmx-aware equivalent of the Referer header for HTMX requests.

func HandleFunc added in v1.1.3

func HandleFunc() xun.HandleFunc

HandleFunc serves the htmx.js library for the htmx extension.

func IsBoosted added in v1.2.1

func IsBoosted(c *xun.Context) bool

IsBoosted reports whether the request was issued via an element that uses hx-boost.

func IsHistoryRestore added in v1.2.1

func IsHistoryRestore(c *xun.Context) bool

IsHistoryRestore reports whether the request was triggered by htmx's local history cache restoration after a miss.

func IsHxRequest added in v1.2.1

func IsHxRequest(c *xun.Context) bool

IsHxRequest reports whether the current request is an HTMX request. It returns true when the "HX-Request" header equals "true".

This is a convenience helper that mirrors (*interceptor).IsHxRequest without requiring callers to construct an interceptor instance. It lets handlers branch on HTMX behavior without registering xun.WithInterceptor, while still using the same header check that the interceptor performs.

func New

func New() xun.Interceptor

New creates a new HTMX interceptor.

func Prompt added in v1.2.1

func Prompt(c *xun.Context) string

Prompt returns the user response to an hx-prompt, or an empty string if no prompt was shown.

func Target added in v1.2.1

func Target(c *xun.Context) string

Target returns the id of the target element sent by htmx, or an empty string if it is not present.

func Trigger added in v1.2.1

func Trigger(c *xun.Context) string

Trigger returns the id of the triggered element sent by htmx, or an empty string if it is not present.

func TriggerName added in v1.2.1

func TriggerName(c *xun.Context) string

TriggerName returns the name attribute of the triggered element sent by htmx, or an empty string if it is not present.

func WriteHeader

func WriteHeader(c *xun.Context, key string, value any)

WriteHeader writes the given value as a header with the given key. The value is marshaled to JSON before being written. If the value is a string, it is written as is.

func WriteLocation added in v1.2.1

func WriteLocation(c *xun.Context, location HxHeader[string])

WriteLocation instructs htmx to perform a client-side redirect that does not do a full page reload, while also replacing the current history entry. It writes the HX-Location header with a JSON-encoded location object, matching the htmx spec.

func WritePushUrl added in v1.2.1

func WritePushUrl(c *xun.Context)

WritePushUrl pushes the current URL of the browser into the history stack. To push a different URL, use WritePushUrlTo.

func WritePushUrlTo added in v1.2.1

func WritePushUrlTo(c *xun.Context, url string)

WritePushUrlTo pushes the given URL into the history stack.

func WriteRedirect added in v1.2.1

func WriteRedirect(c *xun.Context, url string)

WriteRedirect instructs htmx to perform a client-side redirect to url without a full page reload. It writes the HX-Redirect response header and sets the status code to 200 so the body is not rendered.

func WriteRefresh added in v1.2.1

func WriteRefresh(c *xun.Context)

WriteRefresh instructs htmx to do a full refresh of the page.

func WriteReplaceUrl added in v1.2.1

func WriteReplaceUrl(c *xun.Context)

WriteReplaceUrl replaces the current URL in the location bar without adding a new entry to the history stack. To replace with a specific URL, use WriteReplaceUrlTo.

func WriteReplaceUrlTo added in v1.2.1

func WriteReplaceUrlTo(c *xun.Context, url string)

WriteReplaceUrlTo replaces the current URL in the location bar with the given URL.

func WriteReselect added in v1.2.1

func WriteReselect(c *xun.Context, selector string)

WriteReselect sets the CSS selector that picks which part of the response is swapped in. It overrides any existing hx-select on the triggering element.

func WriteReswap added in v1.2.1

func WriteReswap(c *xun.Context, strategy string)

WriteReswap overrides the hx-swap strategy for the response. See https://htmx.org/reference/#response_headers for accepted values such as "innerHTML", "outerHTML", "beforebegin", "afterend", "none", or a swap timing extension like "innerHTML swap:200ms".

func WriteRetarget added in v1.2.1

func WriteRetarget(c *xun.Context, selector string)

WriteRetarget sets the CSS selector that updates the swap target of the response to a different element on the page. It writes the HX-Retarget response header.

func WriteTrigger added in v1.2.1

func WriteTrigger(c *xun.Context, key string, value any)

WriteTrigger sends an HTMX trigger response header so the client fires a custom event after the response is handled. It forwards to WriteHeader so a plain string is emitted as the bare event name and any other value (typically an HxHeader[T] map) is JSON-encoded and sent as the event detail.

Use it with HxTrigger for the default trigger step, or with HxTriggerAfterSettle / HxTriggerAfterSwap to fire on the later steps.

htmx.WriteTrigger(c, htmx.HxTrigger, "item-added")
htmx.WriteTrigger(c, htmx.HxTrigger, htmx.HxHeader[string]{"item-added": "abc"})

Types

type HxHeader

type HxHeader[T any] map[string]T

HxHeader represents a map of string keys to values of any type. It is a generic type that can hold headers for HTMX requests or responses.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL