htmx

package
v0.0.0-...-59c71b3 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Request header that is "true" if the request was made from an element using 'hx-boost'.
	HeaderBoosted = "HX-Boosted"
	// Request header for the current URL of the browser.
	HeaderCurrentURL = "HX-Current-URL"
	// Request header that is “true” if the request is for history restoration after a miss in the local history cache.
	HeaderHistoryRestoreRequest = "HX-History-Restore-Request"
	// Request header for the user response to an hx-prompt.
	HeaderPrompt = "HX-Prompt"
	// Request header that is always “true” for HTMX requests.
	HeaderRequest = "Hx-Request"
	// Request header of the id of the target element if it exists.
	HeaderTarget = "HX-Target"
	// Request header of the name of the triggered element if it exists.
	HeaderTriggerName = "Hx-Trigger-Name"
)

HTTP request headers

View Source
const (
	// Response header that allows you to do a client-side redirect that does not do a full page reload.
	HeaderLocation = "HX-Location"
	// Response header that pushes a new url into the history stack.
	HeaderPushURL = "HX-Push-Url"
	// Response header that can be used to do a client-side redirect to a new location.
	HeaderRedirect = "HX-Redirect"
	// Response header that if set to “true” the client-side will do a full refresh of the page.
	HeaderRefresh = "HX-Refresh"
	// Response header that replaces the current URL in the location bar.
	HeaderReplaceUrl = "HX-Replace-Url"
	// Response header that allows you to specify how the response will be swapped.
	HeaderReswap = "HX-Reswap"
	// Response header that uses a CSS selector that updates the target of the content update to a
	// different element on the page.
	HeaderRetarget = "HX-Retarget"
	// Response header that uses 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.
	HeaderReselect = "HX-Reselect"
	// Response header that allows you to trigger client-side events after the settle step.
	HeaderTriggerAfterSettle = "HX-Trigger-After-Settle"
	// Response header that allows you to trigger client-side events after the swap step.
	HeaderTriggerAfterSwap = "HX-Trigger-After-Swap"
)

HTTP response headers

View Source
const (
	// As a request header: The ID of the triggered element if it exists.
	//
	// As a response header: Allows you to trigger client-side events.
	HeaderTrigger = "HX-Trigger"
)

Common HTTP headers

View Source
const StatusStopPolling int = 286

286 Stop Polling

HTTP status code that tells HTMX to stop polling from a server response.

For more info, see https://htmx.org/docs/#load_polling

Variables

This section is empty.

Functions

func GetCurrentURL

func GetCurrentURL(r *http.Request) (string, bool)

GetCurrentURL returns the current URL that HTMX made this request from.

Returns false if header 'HX-Current-URL' does not exist.

func GetPrompt

func GetPrompt(r *http.Request) (string, bool)

GetPrompt returns the user response to an hx-prompt from a given request.

Returns false if header 'HX-Prompt' does not exist.

For more info, see https://htmx.org/attributes/hx-prompt/

func GetTarget

func GetTarget(r *http.Request) (string, bool)

GetTarget returns the ID of the target element if it exists from a given request.

Returns false if header 'HX-Target' does not exist.

For more info, see https://htmx.org/attributes/hx-target/

func GetTrigger

func GetTrigger(r *http.Request) (string, bool)

GetTrigger returns the ID of the triggered element if it exists from a given request.

Returns false if header 'HX-Trigger' does not exist.

For more info, see https://htmx.org/attributes/hx-trigger/

func GetTriggerName

func GetTriggerName(r *http.Request) (string, bool)

GetTriggerName returns the 'name' of the triggered element if it exists from a given request.

Returns false if header 'HX-Trigger-Name' does not exist.

For more info, see https://htmx.org/attributes/hx-trigger/

func IsBoosted

func IsBoosted(r *http.Request) bool

IsBoosted returns true if the given request was made via an element using 'hx-boost'.

This can be used to add special logic for boosted requests.

Checks if header 'HX-Boosted' is 'true'.

For more info, see https://htmx.org/attributes/hx-boost/

func IsHTMX

func IsHTMX(r *http.Request) bool

IsHTMX returns true if the given request was made by HTMX.

This can be used to add special logic for HTMX requests.

Checks if header 'HX-Request' is 'true'.

func IsHistoryRestoreRequest

func IsHistoryRestoreRequest(r *http.Request) bool

IsHistoryRestoreRequest returns true if the given request is for history restoration after a miss in the local history cache.

Checks if header 'HX-History-Restore-Request' is 'true'.

func RequireHTMX

func RequireHTMX(next http.Handler) http.Handler

RequireHTMX middleware will only pass control to the next handler if the request is HTMX powered. If not, it will return 403: Forbidden response.

func SetupHTMX

func SetupHTMX(next http.Handler) http.Handler

SetupHTMX middleware performs general setup for serving HTMX-powered content.

Types

type Attributes

type Attributes struct {
	// contains filtered or unexported fields
}

Attributes represents htmx hx-* attributes applied to an element.

func NewAttributes

func NewAttributes(options ...AttributesOption) *Attributes

NewAttributes creates a Attributes object for an element with the given options.

func (*Attributes) GetAttributes

func (p *Attributes) GetAttributes() templ.Attributes

GetAttributes returns the hx-* attributes as a templ.Attributes.

func (*Attributes) HasAttribute

func (p *Attributes) HasAttribute(key string) bool

HasAttribute returns a boolean indicating whether there is an attribute with the given key.

func (*Attributes) SetAttribute

func (p *Attributes) SetAttribute(key string, value any)

SetAttribute sets an attribute with the given key to the given value. Any existing value is overridden.

type AttributesOption

type AttributesOption func(*Attributes)

AttributesOption is a functional option to set Properties.

func WithHXInclude

func WithHXInclude(value string) AttributesOption

WithHXInclude sets the hx-include attribute.

func WithHXMethod

func WithHXMethod(method, value string) AttributesOption

WithHXMethod sets the method attribute (i.e., hx-get, hx-post, etc.) to the given value.

func WithHXPushURL

func WithHXPushURL(value any) AttributesOption

WithHXPushURL sets hx-push-url to true.

func WithHXReplaceURL

func WithHXReplaceURL(value any) AttributesOption

WithHXReplaceURL sets hx-replace-url to true.

func WithHXSwap

func WithHXSwap(value string) AttributesOption

WithHXSwap sets the hx-swap attribute.

func WithHXTarget

func WithHXTarget(target string) AttributesOption

WithHXTarget sets the hx-target attribute.

func WithHXTrigger

func WithHXTrigger(value string) AttributesOption

WithHXTrigger sets the hx-trigger attribute.

func WithHXVals

func WithHXVals(vals any) AttributesOption

WithHXVals sets the hx-vals attribute. It can handle a string value directly or a map of values which will get marshaled into a JSON string representation.

type Config

type Config struct {
	// Number of entries to cache in history. Defaults to 10.
	HistoryCacheSize int `json:"historyCacheSize" validate:"omitzero,gte=0"`
	// Whether to issue a full page refresh on history misses rather than use an AJAX request. Defaults to false.
	RefreshOnHistoryMiss *bool `json:"refreshOnHistoryMiss,omitempty"`
	// AllowNestedOOBSwaps configures whether to process OOB swaps on elements that are nested within the main response
	// element.
	AllowNestedOOBSwaps *bool `json:"allowNestedOobSwaps,omitempty"`
	// InlineStyleNonce configures a none to be added to inline styles created by htmx.
	InlineStyleNonce string `json:"inlineStyleNonce,omitzero"`
	// InlineStyleNonce configures a none to be added to inline scripts created by htmx.
	InlineScriptNonce string `json:"inlineScriptNonce,omitzero"`
	// IncludeIndicatorStyles configures whether htmx will dynamically add indicator styles inline for requests.
	IncludeIndicatorStyles *bool `json:"includeIndicatorStyles,omitempty"`
	// HistoryRestoreAsHxRequest configures whether to treat history cache miss full page reload requests as a
	// “HX-Request” by returning this response header. This should always be disabled when using HX-Request header to
	// optionally return partial responses
	HistoryRestoreAsHxRequest *bool `json:"historyRestoreAsHxRequest,omitempty"`
	// GlobalViewTransitions configures whether htmx will use the View Transition API when swapping in new content.
	GlobalViewTransitions *bool `json:"globalViewTransitions,omitempty"`
	// ResponseHandling configures how to handle various HTTP response codes.
	ResponseHandling []*ResponseHandling `json:"responseHandling,omitzero"`
	// Defaults to ‘instant’, the scroll behavior when using the show modifier with hx-swap. The allowed values are
	// instant (scrolling should happen instantly in a single jump), smooth (scrolling should animate smoothly) and auto
	// (scroll behavior is determined by the computed value of scroll-behavior).
	ScrollBehavior string `json:"scrollBehavior,omitzero" validate:"omitzero,oneof=instant smooth auto"`
	// Defaults to false. If it is set to true, the inheritance of attributes is completely disabled and you can
	// explicitly specify the inheritance with the hx-inherit attribute.
	DisableInheritance *bool `json:"disableInheritance,omitempty"`
}

Config defines the htmx config options.

https://htmx.org/docs/#config

type HXLocationRequest

type HXLocationRequest struct {
	// The URL path.
	Path string `json:"path"`
	//  The source element of the request.
	Source string `json:"source,omitzero"`
	// An event that “triggered” the request.
	Event string `json:"event,omitzero"`
	// A JS callback that will handle the response HTML.
	Handler string `json:"handler,omitzero"`
	// The target to swap the response into.
	Target string `json:"target,omitzero"`
	// How the response will be swapped in relative to the target.
	Swap string `json:"swap,omitzero"`
	// Values to submit with the request.
	Values any `json:"values,omitzero"`
	// Headers to submit with the request.
	Headers map[string]string `json:"headers,omitzero"`
	// Allows you to select the content you want swapped from a response.
	Select string `json:"select,omitzero"`
	// Set to 'false' or a path string to prevent or override the URL pushed to browser location history
	Push string `json:"push,omitzero"`
	// A path string to replace the URL in the browser location history
	Replace string `json:"replace,omitzero"`
}

HXLocationRequest defines the value of the HX-Location header.

https://htmx.org/headers/hx-location/

type ID

type ID string

ID represents an id attribute in a HTML element.

func (ID) String

func (a ID) String() string

String returns the id attribute as a string.

func (ID) Target

func (a ID) Target() string

Target returns the id attribute as a target (i.e., for htmx requests). This is the base id string with a "#" prefix.

type ResponseHandling

type ResponseHandling struct {
	// Code is a String representing a regular expression that will be tested against response codes.
	Code string `json:"code"`
	// Swap is true if the response should be swapped into the DOM, false otherwise.
	Swap bool `json:"swap"`
	// Error is true if htmx should treat this response as an error.
	Error bool `json:"error,omitempty,omitzero"`
	// IgnoreTitle is true if htmx should ignore title tags in the response.
	IgnoreTitle bool `json:"ignoreTitle,omitempty,omitzero"`
	// Select is a CSS selector to use to select content from the response.
	Select string `json:"select,omitempty,omitzero"`
	// Target is a CSS selector specifying an alternative target for the response.
	Target string `json:"target,omitempty,omitzero"`
	// SwapOverride is an alternative swap mechanism for the response.
	SwapOverride string `json:"swapOverride,omitempty,omitzero"`
}

ResponseHandling configures how htmx handles different HTTP response codes. When htmx receives a response it will iterate in order over the htmx.config.responseHandling array and test if the code property of a given object, when treated as a Regular Expression, matches the current response. If an entry does match the current response code, it will be used to determine if and how the response will be processed.

https://htmx.org/docs/#response-handling

Jump to

Keyboard shortcuts

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