Documentation ¶
Overview ¶
Package htmx provides utilities to build HTMX-powered web applications.
Index ¶
- Constants
- func GetCurrentURL(r *http.Request) (string, bool)
- func GetPrompt(r *http.Request) (string, bool)
- func GetTarget(r *http.Request) (string, bool)
- func GetTrigger(r *http.Request) (string, bool)
- func GetTriggerName(r *http.Request) (string, bool)
- func IsBoosted(r *http.Request) bool
- func IsHTMX(r *http.Request) bool
- func IsHistoryRestoreRequest(r *http.Request) bool
- func Trigger(eventName string) triggerPlain
- func TriggerDetail(eventName string, detailValue string) triggerDetail
- func TriggerObject(eventName string, detailObject any) triggerObject
- type Direction
- type EventTrigger
- type LocationContext
- type Response
- func (r Response) AddTrigger(trigger ...EventTrigger) Response
- func (r Response) AddTriggerAfterSettle(trigger ...EventTrigger) Response
- func (r Response) AddTriggerAfterSwap(trigger ...EventTrigger) Response
- func (r Response) Clone() Response
- func (r Response) Headers() (map[string]string, error)
- func (r Response) Location(path string) Response
- func (r Response) LocationWithContext(path string, ctx LocationContext) Response
- func (r Response) MustRenderHTML(w http.ResponseWriter, html template.HTML)
- func (r Response) MustRenderTempl(ctx context.Context, w http.ResponseWriter, c templComponent)
- func (r Response) MustWrite(w http.ResponseWriter)
- func (r Response) PreventPushURL() Response
- func (r Response) PreventReplaceURL() Response
- func (r Response) PushURL(url string) Response
- func (r Response) Redirect(path string) Response
- func (r Response) Refresh(shouldRefresh bool) Response
- func (r Response) RenderHTML(w http.ResponseWriter, html template.HTML) (int, error)
- func (r Response) RenderTempl(ctx context.Context, w http.ResponseWriter, c templComponent) error
- func (r Response) ReplaceURL(url string) Response
- func (r Response) Reselect(cssSelector string) Response
- func (r Response) Reswap(s SwapStrategy) Response
- func (r Response) Retarget(cssSelector string) Response
- func (r Response) StatusCode(statusCode int) Response
- func (r Response) Write(w http.ResponseWriter) error
- type SwapStrategy
- func (s SwapStrategy) After(duration time.Duration) SwapStrategy
- func (s SwapStrategy) FocusScroll(shouldFocus bool) SwapStrategy
- func (s SwapStrategy) IgnoreTitle(shouldIgnore bool) SwapStrategy
- func (s SwapStrategy) Scroll(direction Direction) SwapStrategy
- func (s SwapStrategy) ScrollOn(cssSelector string, direction Direction) SwapStrategy
- func (s SwapStrategy) ScrollWindow(direction Direction) SwapStrategy
- func (s SwapStrategy) SettleAfter(duration time.Duration) SwapStrategy
- func (s SwapStrategy) Show(direction Direction) SwapStrategy
- func (s SwapStrategy) ShowNone() SwapStrategy
- func (s SwapStrategy) ShowOn(cssSelector string, direction Direction) SwapStrategy
- func (s SwapStrategy) ShowWindow(direction Direction) SwapStrategy
- func (s SwapStrategy) Transition(shouldTransition bool) SwapStrategy
Constants ¶
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
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
const ( // Direction value for the 'scroll' and 'show' swap modifier methods. Top direction = "top" // Direction value for the 'scroll' and 'show' swap modifier methods. Bottom direction = "bottom" )
Direction values for the htmx.SwapStrategy 'scroll' and 'show' modifier methods.
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
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 ¶
GetCurrentURL returns the current URL that HTMX made this request from.
Returns false if header 'HX-Current-URL' does not exist.
func GetPrompt ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 Trigger ¶
func Trigger(eventName string) triggerPlain
Trigger returns an event trigger with no additional details.
Example:
htmx.Trigger("myEvent")
Output header:
HX-Trigger: myEvent
For more info, see https://htmx.org/headers/hx-trigger/
func TriggerDetail ¶ added in v0.2.0
TriggerDetail returns an event trigger with one detail string. Will be encoded as JSON.
Example:
htmx.TriggerDetail("showMessage", "Here Is A Message")
Output header:
HX-Trigger: {"showMessage":"Here Is A Message"}
For more info, see https://htmx.org/headers/hx-trigger/
func TriggerObject ¶ added in v0.2.0
TriggerObject returns an event trigger with a given detail object that **must** be serializable to JSON.
Structs with JSON tags can work, and so does `map[string]string` values which are safe to serialize.
Example:
htmx.TriggerObject("showMessage", map[string]string{ "level": "info", "message": "Here Is A Message", })
Output header:
HX-Trigger: {"showMessage":{"level" : "info", "message" : "Here Is A Message"}}
For more info, see https://htmx.org/headers/hx-trigger/
Types ¶
type Direction ¶ added in v0.4.0
type Direction interface {
// contains filtered or unexported methods
}
Direction is a value for the htmx.SwapStrategy 'scroll' and 'show' modifier methods.
Possible values are htmx.Top and htmx.Bottom.
type EventTrigger ¶
type EventTrigger interface {
// contains filtered or unexported methods
}
EventTrigger gives an HTMX response directives to triggers events on the client side.
type LocationContext ¶
type LocationContext struct { // The source element of the request. Source string // An event that “triggered” the request. Event string // A JavaScript callback that will handle the response HTML. Handler string // The target to swap the response into. Target string // How the response will be swapped in relative to the target. Swap SwapStrategy // Values to submit with the request. Values map[string]string // Headers to submit with the request Headers map[string]string // Allows you to select the content you want swapped from a response. Select string }
A context object that is used by htmx.Response.LocationWithContext to finely determine the behavior of client-side redirection.
In the browser, these are the parameters that will be used by 'htmx.ajax()'.
For more info, see https://htmx.org/headers/hx-location/
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response contains HTMX headers to write to a response.
func NewResponse ¶
func NewResponse() Response
NewResponse returns a new HTMX response header writer.
Any subsequent method calls that write to the same header will overwrite the last set header value.
func (Response) AddTrigger ¶
func (r Response) AddTrigger(trigger ...EventTrigger) Response
AddTrigger adds trigger(s) for events that trigger as soon as the response is received.
This can be called multiple times so you can add as many triggers as you need.
Sets the 'HX-Trigger' header.
For more info, see https://htmx.org/headers/hx-trigger/
func (Response) AddTriggerAfterSettle ¶
func (r Response) AddTriggerAfterSettle(trigger ...EventTrigger) Response
AddTriggerAfterSettle adds trigger(s) for events that trigger after the settling step.
This can be called multiple times so you can add as many triggers as you need.
Sets the 'HX-Trigger-After-Settle' header.
For more info, see https://htmx.org/headers/hx-trigger/
func (Response) AddTriggerAfterSwap ¶
func (r Response) AddTriggerAfterSwap(trigger ...EventTrigger) Response
AddTriggerAfterSwap adds trigger(s) for events that trigger after the swap step.
This can be called multiple times so you can add as many triggers as you need.
Sets the 'HX-Trigger-After-Swap' header.
For more info, see https://htmx.org/headers/hx-trigger/
func (Response) Clone ¶
Clone returns a clone of this HTMX response writer, preventing any mutation on the original response.
func (Response) Headers ¶
Headers returns a copied map of the headers. Any modifications to the returned headers will not affect the headers in this struct.
func (Response) Location ¶
Location allows you to do a client-side redirect that does not do a full page reload.
If you want to redirect to a specific target on the page rather than the default of document.body, you can use htmx.Response.LocationWithContext.
Sets the 'HX-Location' header.
For more info, see https://htmx.org/headers/hx-location/
func (Response) LocationWithContext ¶
func (r Response) LocationWithContext(path string, ctx LocationContext) Response
LocationWithContext allows you to do a client-side redirect that does not do a full page reload, redirecting to a specific target on the page with the given context.
For simple redirects, you can just use htmx.Response.Location.
Sets the 'HX-Location' header.
For more info, see https://htmx.org/headers/hx-location/
func (Response) MustRenderHTML ¶ added in v0.5.0
func (r Response) MustRenderHTML(w http.ResponseWriter, html template.HTML)
MustRenderHTML renders an HTML document fragment along with the defined HTMX headers, otherwise it panics.
Under the hood this uses Response.RenderHTML.
func (Response) MustRenderTempl ¶ added in v0.5.0
func (r Response) MustRenderTempl(ctx context.Context, w http.ResponseWriter, c templComponent)
MustRenderTempl renders a Templ component along with the defined HTMX headers, otherwise it panics.
Under the hood this uses Response.RenderTempl.
func (Response) MustWrite ¶ added in v0.5.0
func (r Response) MustWrite(w http.ResponseWriter)
MustWrite applies the defined HTMX headers to a given response writer, otherwise it panics.
Under the hood this uses Response.Write.
func (Response) PreventPushURL ¶
PreventPushURL prevents the browser’s history from being updated.
Sets the same header as htmx.Response.PushURL, overwriting previous set headers.
Sets the 'HX-Push-Url' header.
For more info, see https://htmx.org/headers/hx-push-url/
func (Response) PreventReplaceURL ¶
PreventReplaceURL prevents the browser’s current URL from being updated.
Sets the same header as htmx.Response.ReplaceURL, overwriting previous set headers.
Sets the 'HX-Replace-Url' header to 'false'.
For more info, see https://htmx.org/headers/hx-replace-url/
func (Response) PushURL ¶
PushURL pushes a new URL into the browser location history.
Sets the same header as htmx.Response.PreventPushURL, overwriting previous set headers.
Sets the 'HX-Push-Url' header.
For more info, see https://htmx.org/headers/hx-push-url/
func (Response) Redirect ¶
Redirect does a client-side redirect to a new location.
Sets the 'HX-Redirect' header.
func (Response) Refresh ¶
If set to true, Refresh makes the client-side do a full refresh of the page.
Sets the 'HX-Refresh' header.
func (Response) RenderHTML ¶ added in v0.5.0
RenderHTML renders an HTML document fragment along with the defined HTMX headers.
func (Response) RenderTempl ¶
RenderTempl renders a Templ component along with the defined HTMX headers.
func (Response) ReplaceURL ¶
ReplaceURL replaces the current URL in the browser location history.
Sets the same header as htmx.Response.PreventReplaceURL, overwriting previous set headers.
Sets the 'HX-Replace-Url' header.
For more info, see https://htmx.org/headers/hx-replace-url/
func (Response) Reselect ¶
Reselect accepts 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.
Sets the 'HX-Reselect' header.
For more info, see https://htmx.org/attributes/hx-select/
func (Response) Reswap ¶
func (r Response) Reswap(s SwapStrategy) Response
Reswap allows you to specify how the response will be swapped.
Sets the 'HX-Reswap' header.
For more info, see https://htmx.org/attributes/hx-swap/
func (Response) Retarget ¶
Retarget accepts a CSS selector that updates the target of the content update to a different element on the page. Overrides an existing 'hx-select' on the triggering element.
Sets the 'HX-Retarget' header.
For more info, see https://htmx.org/attributes/hx-target/
func (Response) StatusCode ¶
StatusCode sets the HTTP response header of this response.
If StatusCode is not called, the default status code will be 200 OK.
type SwapStrategy ¶ added in v0.4.0
type SwapStrategy string
SwapStrategy is an 'hx-swap' value that determines the swapping strategy of htmx.Response.Reswap and LocationContext.
SwapStrategy methods add modifiers to change the behavior of the swap.
const ( // Replace the inner html of the target element. // // Valid value for [Response.Reswap]. SwapInnerHTML SwapStrategy = "innerHTML" // Replace the entire target element with the response. // // Valid value for [Response.Reswap]. SwapOuterHTML SwapStrategy = "outerHTML" // Insert the response before the target element. // // Valid value for [Response.Reswap]. SwapBeforeBegin SwapStrategy = "beforebegin" // Insert the response before the first child of the target element. // // Valid value for [Response.Reswap]. SwapAfterBegin SwapStrategy = "afterbegin" // Insert the response after the last child of the target element. // // Valid value for [Response.Reswap]. SwapBeforeEnd SwapStrategy = "beforeend" // Insert the response after the target element. // // Valid value for [Response.Reswap]. SwapAfterEnd SwapStrategy = "afterend" // Deletes the target element regardless of the response. // // Valid value for [Response.Reswap]. SwapDelete SwapStrategy = "delete" // Does not append content from response (out of band items will still be processed). // // Valid value for [Response.Reswap]. SwapNone SwapStrategy = "none" // Uses the default swap style (default in HTMX is [SwapInnerHTML]). // This value is useful for adding modifiers to the [SwapStrategy] // through methods // without changing the default swap style. // // // Valid value for [Response.Reswap]. SwapDefault SwapStrategy = "" )
func (SwapStrategy) After ¶ added in v0.4.0
func (s SwapStrategy) After(duration time.Duration) SwapStrategy
After modifies the amount of time that HTMX will wait after receiving a response to swap the content.
Adds the 'swap:<duration>' modifier.
For more info, see https://htmx.org/attributes/hx-swap/
func (SwapStrategy) FocusScroll ¶ added in v0.4.0
func (s SwapStrategy) FocusScroll(shouldFocus bool) SwapStrategy
FocusScroll enables focus scroll to automatically scroll to the focused element after a request.
Adds the 'focusScroll:<true | false>' modifier.
For more info, see https://htmx.org/attributes/hx-swap/
func (SwapStrategy) IgnoreTitle ¶ added in v0.4.0
func (s SwapStrategy) IgnoreTitle(shouldIgnore bool) SwapStrategy
IgnoreTitle prevents HTMX from updating the title of the page if there is a '<title>' tag in the response content.
By default, HTMX updates the title.
Adds the 'ignoreTitle:<true | false>' modifier.
For more info, see https://htmx.org/attributes/hx-swap/
func (SwapStrategy) Scroll ¶ added in v0.4.0
func (s SwapStrategy) Scroll(direction Direction) SwapStrategy
Scroll changes the scrolling behavior based on the given direction.
Scroll(htmx.Top) scrolls to the top of the swapped-in element.
Scroll(htmx.Bottom) scrolls to the bottom of the swapped-in element.
Adds the 'scroll:<direction ("top" | "bottom")>' modifier.
For more info, see https://htmx.org/attributes/hx-swap/
func (SwapStrategy) ScrollOn ¶ added in v0.4.0
func (s SwapStrategy) ScrollOn(cssSelector string, direction Direction) SwapStrategy
ScrollOn changes the scrolling behavior based on the given direction and CSS selector.
ScrollOn(cssSelector, htmx.Top) scrolls to the top of the element found by the selector.
ScrollOn(cssSelector, htmx.Bottom) scrolls to the bottom of the element found by the selector.
Adds the 'scroll:<cssSelector>:<direction ("top" | "bottom")>' modifier.
For more info, see https://htmx.org/attributes/hx-swap/
func (SwapStrategy) ScrollWindow ¶ added in v0.4.0
func (s SwapStrategy) ScrollWindow(direction Direction) SwapStrategy
ScrollWindow changes the scrolling behavior based on the given direction.
ScrollWindow(htmx.Top) scrolls to the very top of the window.
ScrollWindow(htmx.Bottom) scrolls to the very bottom of the window.
Adds the 'scroll:window:<direction ("top" | "bottom")>' modifier.
For more info, see https://htmx.org/attributes/hx-swap/
func (SwapStrategy) SettleAfter ¶ added in v0.4.0
func (s SwapStrategy) SettleAfter(duration time.Duration) SwapStrategy
SettleAfter modifies the amount of time that HTMX will wait after the swap before executing the settle logic.
Adds the 'settle:<duration>' modifier.
For more info, see https://htmx.org/attributes/hx-swap/
func (SwapStrategy) Show ¶ added in v0.4.0
func (s SwapStrategy) Show(direction Direction) SwapStrategy
Show changes the show behavior based on the given direction.
Show(htmx.Top) shows the top of the swapped-in element.
Show(htmx.Bottom) shows the bottom of the swapped-in element.
Adds the 'show:<direction ("top" | "bottom")>' modifier.
For more info, see https://htmx.org/attributes/hx-swap/
func (SwapStrategy) ShowNone ¶ added in v0.4.0
func (s SwapStrategy) ShowNone() SwapStrategy
ShowNone disables 'show'.
Adds the 'show:none' modifier.
For more info, see https://htmx.org/attributes/hx-swap/
func (SwapStrategy) ShowOn ¶ added in v0.4.0
func (s SwapStrategy) ShowOn(cssSelector string, direction Direction) SwapStrategy
ShowOn changes the show behavior based on the given direction and CSS selector.
ShowOn(cssSelector, htmx.Top) shows the top of the element found by the selector.
ShowOn(cssSelector, htmx.Bottom) shows the bottom of the element found by the selector.
Adds the 'show:<cssSelector>:<direction ("top" | "bottom")>' modifier.
For more info, see https://htmx.org/attributes/hx-swap/
func (SwapStrategy) ShowWindow ¶ added in v0.4.0
func (s SwapStrategy) ShowWindow(direction Direction) SwapStrategy
ShowWindow changes the show behavior based on the given direction.
ScrollWindow(htmx.Top) shows the very top of the window.
ScrollWindow(htmx.Bottom) shows the very bottom of the window.
Adds the 'show:window:<direction ("top" | "bottom")>' modifier.
For more info, see https://htmx.org/attributes/hx-swap/
func (SwapStrategy) Transition ¶ added in v0.4.0
func (s SwapStrategy) Transition(shouldTransition bool) SwapStrategy
Transition makes the swap use the new View Transitions API.
Adds the 'transition:<true | false>' modifier.
For more info, see https://htmx.org/attributes/hx-swap/