Documentation
¶
Index ¶
- Constants
- func Event(name string, data ...any) event
- func GetCurrentUrl(r *http.Request) string
- func GetPrompt(r *http.Request) string
- func GetTarget(r *http.Request) string
- func GetTrigger(r *http.Request) string
- func GetTriggerName(r *http.Request) string
- func IsBoosted(r *http.Request) bool
- func IsHistoryRestoreRequest(r *http.Request) bool
- func IsHtmx(r *http.Request) bool
- func IsRequest(r *http.Request) bool
- func Location(path string, properties ...property) responseOptionFunc
- func Refresh() responseOptionFunc
- func Response(w http.ResponseWriter, options ...ResponseOption) error
- func Swap[T string | Reswap](swap T) propertyFunc
- func Trigger(events ...event) responseOptionFunc
- func TriggerAfterSettle(events ...event) responseOptionFunc
- func TriggerAfterSwap(events ...event) responseOptionFunc
- func Values(values any) propertyFunc
- type EventName
- type Handler
- type Headers
- type HtmxResponse
- type PushUrl
- type Redirect
- type ReplaceUrl
- type Reselect
- type ResponseOption
- type Reswap
- func (s Reswap) FocusScroll(focus bool) Reswap
- func (s Reswap) IgnoreTitle() Reswap
- func (s Reswap) Scroll(target string) Reswap
- func (s Reswap) Settle(dur time.Duration) Reswap
- func (s Reswap) Show(target string) Reswap
- func (s Reswap) Swap(dur time.Duration) Reswap
- func (s Reswap) Transition() Reswap
- type Retarget
- type Select
- type Source
- type Status
- type Target
Constants ¶
const ( // HxBoosted indicates that the request is a boosted request. // // See https://htmx.org/reference/#request_headers for more details. // // Use the IsBoosted() function to check this header in the request. HxBoosted = "Hx-Boosted" // HxCurrentUrl represents the current URL of the browser. // // See https://htmx.org/reference/#request_headers for more details. // // Use the GetCurrentUrl() function to fetch this header from the request. HxCurrentUrl = "Hx-Current-Url" // HxHistoryRestoreRequest is "true" if the request is for history restoration after a miss in the local history cache. // // See https://htmx.org/reference/#request_headers for more details. // // Use the IsHistoryRestoreRequest() function to check this header in the request. HxHistoryRestoreRequest = "Hx-History-Restore-Request" // HxPrompt captures the user's response to an HX-Prompt. // // See https://htmx.org/reference/#request_headers for more details. // // Use the GetPrompt() function to fetch this header from the request. HxPrompt = "Hx-Prompt" // HxRequest is always "true" if the request is an HTMX request. // // See https://htmx.org/reference/#request_headers for more details. // // Use the IsRequest() function to check this header in the request. HxRequest = "Hx-Request" // HxTarget identifies the ID of the target element, if it exists. // // See https://htmx.org/reference/#request_headers for more details. // // Use the GetTarget() function to fetch this header from the request. HxTarget = "Hx-Target" // HxTriggerName denotes the name of the triggered element, if it exists. // // See https://htmx.org/reference/#request_headers for more details. // // Use the GetTriggerName() function to fetch this header from the request. HxTriggerName = "Hx-Trigger-Name" )
Request Headers
const ( // HxLocation allows for client-side redirects without a full page reload. // // See https://htmx.org/reference/#response_headers for more details. // // Use the Location() option to set this header in the response. HxLocation = "Hx-Location" // HxPushUrl pushes a new URL into the history stack. // // See https://htmx.org/reference/#response_headers for more details. // // Use the PushUrl() option to set this header in the response. HxPushUrl = "Hx-Push-Url" // HxRedirect can be used for client-side redirects that require a full page reload. // // See https://htmx.org/reference/#response_headers for more details. // // Use the Redirect() option to set this header in the response. HxRedirect = "Hx-Redirect" // HxRefresh when set to "true", triggers a full refresh of the client-side page. // // See https://htmx.org/reference/#response_headers for more details. // // Use the Refresh() option to set this header in the response. HxRefresh = "Hx-Refresh" // HxReplaceUrl replaces the current URL in the location bar. // // See https://htmx.org/reference/#response_headers for more details. // // Use the ReplaceUrl() option to set this header in the response. HxReplaceUrl = "Hx-Replace-Url" // HxReswap specifies how the response will be swapped. // // See https://htmx.org/reference/#response_headers for more details. // // Use the Reswap() option to set this header in the response. HxReswap = "Hx-Reswap" // HxRetarget updates the target of the content update to a different element on the page using a CSS selector. // // See https://htmx.org/reference/#response_headers for more details. // // Use the Retarget() option to set this header in the response. HxRetarget = "Hx-Retarget" // HxReselect allows selection of a specific part of the response to be swapped in, using a CSS selector. It overrides any existing hx-select on the triggering element. // // See https://htmx.org/reference/#response_headers for more details. // // Use the Reselect() option to set this header. HxReselect = "Hx-Reselect" // HxTriggerAfterSettle triggers client-side events after the settle step. // // See https://htmx.org/reference/#response_headers for more details. // // Use the TriggerAfterSettle() option to set this header in the response. HxTriggerAfterSettle = "Hx-Trigger-After-Settle" // HxTriggerAfterSwap triggers client-side events after the swap step. // // See https://htmx.org/reference/#response_headers for more details. // // Use the TriggerAfterSwap() option to set this header in the response. HxTriggerAfterSwap = "Hx-Trigger-After-Reswap" )
Response Headers
const ( // HxTrigger // (in request) the id of the trigger element if it exists // (in response) allows you to trigger events on the client // // More details: https://htmx.org/reference/#request_headers and https://htmx.org/reference/#response_headers // // - Use the GetTrigger() function to fetch this header from the request // - Use the Trigger(...events) option to set this header on the response HxTrigger = "HX-Trigger" )
Request & Response Headers
Variables ¶
This section is empty.
Functions ¶
func Event ¶
Event creates an event to pass to one of the Trigger options.
Simple named event:
htmx.Event("myEvent") // Returns {"myEvent":null}
Named event with string data:
htmx.Event("myEvent", "myData") // Returns {"myEvent":"myData"}
Named event with multiple data items (treated like an array):
htmx.Event("myEvent", "myData1", "myData2") // Returns {"myEvent":["myData1","myData2"]}
Named event with a struct or map (treated like an object):
htmx.Event("myEvent", map[string]string{"myKey": "myValue"}) // Returns {"myEvent":{"myKey":"myValue"}}
func GetCurrentUrl ¶
GetCurrentUrl extracts the HX-Current-URL header from an HTTP request.
It returns the current URL of the browser if the header exists. If the header is not present, it returns an empty string.
func GetPrompt ¶
GetPrompt extracts the HX-Prompt header from an HTTP request.
It returns the user response to an hx-prompt if the header exists. If the header is not present, it returns an empty string.
func GetTarget ¶
GetTarget extracts the HX-Target header from an HTTP request.
It returns the ID of the target element if the header exists. If the header is not present, it returns an empty string.
func GetTrigger ¶
GetTrigger extracts the HX-Trigger header from an HTTP request.
It returns the ID of the trigger element if the header exists. If the header is not present, it returns an empty string.
func GetTriggerName ¶
GetTriggerName extracts the HX-Trigger-Name header from an HTTP request.
It returns the name of the triggered element if the header exists. If the header is not present, it returns an empty string.
func IsBoosted ¶
IsBoosted checks the HX-Boosted header
Returns true if the request is a boosted request
func IsHistoryRestoreRequest ¶
IsHistoryRestoreRequest determines if an HTTP request is a history restore request.
It checks the presence of the HX-History-Restore-Request header in the request. Returns true if the header is present, otherwise returns false.
func IsHtmx ¶ added in v0.1.1
IsHtmx determines if an HTTP request is an HTMX request.
Does the same thing as IsRequest, only with a more user-friendly name.
func IsRequest ¶
IsRequest determines if an HTTP request is an HTMX request.
It checks the presence of the HX-Request header in the request. Returns true if the header is present, otherwise returns false.
func Location ¶
func Location(path string, properties ...property) responseOptionFunc
Location sets the HX-Location header.
This header is used for client-side redirection without a full page reload. Additional properties like Target can be specified for more complex behaviors.
For more details, see: https://htmx.org/headers/hx-location
Simple redirection example:
htmx.Response(w, htmx.Location("/test")) // Sets HX-Location header to "/test"
Redirection with additional target example:
htmx.Response(w, htmx.Location("/test", htmx.Target("#testdiv"), )) // Sets HX-Location header to a JSON object: {"path":"/test","target":"#testdiv"}
func Refresh ¶
func Refresh() responseOptionFunc
Refresh sets the HX-Refresh header.
When set to "true", it triggers a full refresh of the client-side page. Note: This function always sets it to "true".
Example usage:
htmx.Response(w, htmx.Refresh()) // Sets the HX-Refresh header to "true".
func Response ¶
func Response(w http.ResponseWriter, options ...ResponseOption) error
Response modifies the http.ResponseWriter to add HTMX headers and status codes.
The following options are available:
- Status(int) | StatusStopPolling: Sets the HTTP status code of the HTMX response.
- Location(path, ...properties): Enables client-side redirection without a full page reload.
- PushUrl(string): Pushes a new URL into the history stack.
- Redirect(string): Performs a client-side redirect with a full page reload.
- Refresh(bool): If set to "true", triggers a full refresh of the client-side page.
- ReplaceUrl(string): Replaces the current URL in the location bar.
- Reswap(string) | {Swap constants}: Specifies how the response will be swapped.
- Retarget(string): A CSS selector to update the target of the content update to a different page element.
- Reselect(string): A CSS selector to select a part of the response to be swapped in, overriding existing hx-select on the triggering element.
- Trigger(...events): Triggers client-side events.
- TriggerAfterSettle(...events): Triggers client-side events after the settle step.
- TriggerAfterSwap(...events): Triggers client-side events after the swap step.
func Swap ¶
Swap sets the 'swap' property of the HX-Location header.
Either a string or a Reswap constant can be used.
More details: https://htmx.org/headers/hx-location
func Trigger ¶
func Trigger(events ...event) responseOptionFunc
Trigger allows you to trigger events on the client
More details: https://htmx.org/reference/#response_headers
Use Event to create events to pass to this option.
Simple example:
htmx.Response(w, htmx.Trigger(htmx.Event("myEvent"))) // Sets HX-Trigger header to {"myEvent":null}
Example with data:
htmx.Response(w, htmx.Trigger(htmx.Event("myEvent", "myData"))) // Sets HX-Trigger header to {"myEvent":"myData"}
Example with multiple events:
htmx.Response(w, htmx.Trigger( htmx.Event("myEvent", "myData"), htmx.Event("myOtherEvent", "myOtherData"), )) // Sets HX-Trigger header to {"myEvent":"myData","myOtherEvent":"myOtherData"}
See also: TriggerAfterSettle and TriggerAfterSwap
func TriggerAfterSettle ¶
func TriggerAfterSettle(events ...event) responseOptionFunc
TriggerAfterSettle triggers client-side events after the settle step.
More details: https://htmx.org/reference/#response_headers
For more details, see: Trigger
func TriggerAfterSwap ¶
func TriggerAfterSwap(events ...event) responseOptionFunc
TriggerAfterSwap triggers client-side events after the swap step.
More details: https://htmx.org/reference/#response_headers
For more details, see: Trigger
func Values ¶
func Values(values any) propertyFunc
Values sets the 'values' property of the HX-Location header.
Accepts any type, but a map[string]any or a struct with JSON tags is recommended.
More details: https://htmx.org/headers/hx-location
Types ¶
type EventName ¶
type EventName string
EventName sets the 'event' property of the HX-Location header.
More details: https://htmx.org/headers/hx-location
type Handler ¶
type Handler string
Handler sets the 'handler' property of the HX-Location header.
More details: https://htmx.org/headers/hx-location
type Headers ¶
Headers sets the 'headers' property of the HX-Location header.
Accepts a map[string]string.
More details: https://htmx.org/headers/hx-location
type HtmxResponse ¶
type HtmxResponse struct {
// contains filtered or unexported fields
}
HtmxResponse is a struct that contains the headers and status code to be returned to the client
This is helpful for using HTMX with a framework that doesn't implement the stdlib http.ResponseWriter
func BuildResponse ¶
func BuildResponse(options ...ResponseOption) (response *HtmxResponse, err error)
BuildResponse creates a new HtmxResponse from the provided options.
It can be used to create a response helper for your own HTTP library.
Several libraries have already been implemented:
- Echo: import github.com/stackus/htmx/echohtmx
- Fiber: import github.com/stackus/htmx/fiberhtmx
- Gin: import github.com/stackus/htmx/ginhtmx
func (HtmxResponse) Headers ¶
func (r HtmxResponse) Headers() map[string]string
func (HtmxResponse) StatusCode ¶
func (r HtmxResponse) StatusCode() int
type PushUrl ¶
type PushUrl string
PushUrl sets the HX-Push-Url header.
It pushes a new URL into the history stack.
Example usage:
htmx.Response(w, htmx.PushUrl("/new-url-location")) // Sets the HX-Push-Url header to "/new-url-location".
type Redirect ¶
type Redirect string
Redirect sets the HX-Redirect header.
It is used for client-side redirects that require a full page reload.
Example usage:
htmx.Response(w, htmx.Redirect("/new-url-location")) // Sets the HX-Redirect header to "/new-url-location".
type ReplaceUrl ¶
type ReplaceUrl string
ReplaceUrl sets the HX-Replace-Url header.
It replaces the current URL in the location bar.
Example usage:
htmx.Response(w, htmx.ReplaceUrl("/new-url-location")) // Sets the HX-Replace-Url header to "/new-url-location".
type Reselect ¶
type Reselect string
Reselect sets the HX-Reselect header.
This option designates a CSS selector to determine which part of the response should be used for swapping in, effectively overriding any existing hx-select on the triggering element.
Example usage:
htmx.Response(w, htmx.Reselect("#new-target")) // Sets the HX-Reselect header to "#new-target".
type ResponseOption ¶ added in v0.2.0
type ResponseOption interface {
// contains filtered or unexported methods
}
ResponseOption is an interface that can be used to set the headers and status code of the response
type Reswap ¶
type Reswap string
Reswap specifies how the response will be swapped in an HTMX request.
Use the Reswap() directly to set this header in the response, or choose from the constants below for common swap styles.
The constants correspond to the different swap styles:
- SwapInnerHtml: Replaces the inner HTML of the target element.
- SwapOuterHtml: Replaces the entire target element with the response.
- SwapBeforeBegin: Inserts the response before the target element.
- SwapAfterBegin: Inserts the response before the first child of the target element.
- SwapBeforeEnd: Inserts the response after the last child of the target element.
- SwapAfterEnd: Inserts the response after the target element.
- SwapDelete: Deletes the target element, regardless of the response.
- SwapNone: Does not append content from the response (out-of-band items will still be processed).
For more details, see: https://htmx.org/attributes/hx-swap
Simple usage:
htmx.Response(w, htmx.Reswap("innerHTML")) // Sets HX-Reswap header to "innerHTML"
Constant usage example (same as above):
htmx.Response(w, htmx.SwapInnerHtml) // Also sets HX-Reswap header to "innerHTML"
Constant usage example (with modifiers):
htmx.Response(w, htmx.SwapInnerHtml.Swap(1*time.Second).Settle(2*time.Second)) // Sets HX-Reswap header to "innerHTML swap:1s settle:2s"
const ( // SwapInnerHtml replace the inner HTML of the target element SwapInnerHtml Reswap = "innerHTML" // SwapOuterHtml replace the entire target element with the response SwapOuterHtml Reswap = "outerHTML" // SwapAfterBegin insert the response before the target element SwapBeforeBegin Reswap = "beforebegin" // SwapAfterBegin insert the response before the first child of the target element SwapAfterBegin Reswap = "afterbegin" // SwapBeforeEnd insert the response after the last child of the target element SwapBeforeEnd Reswap = "beforeend" // SwapAfterEnd insert the response after the target element SwapAfterEnd Reswap = "afterend" // SwapDelete deletes the target element regardless of the response SwapDelete Reswap = "delete" // SwapNone does not append content from response (out of band items will still be processed) SwapNone Reswap = "none" )
Reswap constants
func (Reswap) FocusScroll ¶
FocusScroll (reswap header modifier) can be used to enable to disable scrolling to the element after swapping
More details: https://htmx.org/attributes/hx-swap/#focus-scroll
Example usage:
htmx.Response(w, htmx.SwapInnerHtml.FocusScroll(true)) // Sets HX-Reswap header to "innerHTML focus-scroll:true"
func (Reswap) IgnoreTitle ¶
IgnoreTitle (reswap header modifier) is used to ignore any <title> tags in the response
More details: https://htmx.org/attributes/hx-swap/#ignore-title
Example usage:
htmx.Response(w, htmx.SwapInnerHtml.IgnoreTitle()) // Sets HX-Reswap header to "innerHTML ignoreTitle:true"
func (Reswap) Scroll ¶
Scroll (reswap header modifier) is used to scroll to the "top", "bottom", a specific element after swapping
More details: https://htmx.org/attributes/hx-swap/#scrolling-scroll-show
Example usage:
htmx.Response(w, htmx.SwapInnerHtml.Scroll("top")) // Sets HX-Reswap header to "innerHTML scroll:top" htmx.Response(w, htmx.SwapInnerHtml.Scroll("#another-div:top")) // Sets HX-Reswap header to "innerHTML scroll:#another-div:top"
func (Reswap) Settle ¶
Settle (reswap header modifier) is used to set a time to wait after swapping before triggering the settle step
More details: https://htmx.org/attributes/hx-swap/#timing-swap-settle
Example usage:
htmx.Response(w, htmx.SwapInnerHtml.Settle(1*time.Second)) // Sets HX-Reswap header to "innerHTML settle:1s"
func (Reswap) Show ¶
Show (reswap header modifier) is used to show the "top", "bottom", or a specific element after swapping
More details: https://htmx.org/attributes/hx-swap/#scrolling-scroll-show
Example usage:
htmx.Response(w, htmx.SwapInnerHtml.Show("top")) // Sets HX-Reswap header to "innerHTML show:top" htmx.Response(w, htmx.SwapInnerHtml.Show("#another-div:top")) // Sets HX-Reswap header to "innerHTML show:#another-div:top"
func (Reswap) Swap ¶
Swap (reswap header modifier) is used to set a time wait after receiving a response before swapping the content
More details: https://htmx.org/attributes/hx-swap/#timing-swap-settle
Example usage:
htmx.Response(w, htmx.SwapInnerHtml.Swap(1*time.Second)) // Sets HX-Reswap header to "innerHTML swap:1s"
func (Reswap) Transition ¶
Transition (reswap header modifier) allows you to specify the use of the View Transition API when a swap occurs
More details: https://htmx.org/attributes/hx-swap/#transition-transition
Example usage:
htmx.Response(w, htmx.SwapInnerHtml.Transition()) // Sets HX-Reswap header to "innerHTML transition:true"
type Retarget ¶
type Retarget string
Retarget sets the HX-Retarget header.
This option specifies a new CSS selector to redirect the content update to a different element on the page.
Example usage:
htmx.Response(w, htmx.Retarget("#new-target")) // Sets the HX-Retarget header to "#new-target".
type Select ¶
type Select string
Select sets the 'select' property of the HX-Location header.
More details: https://htmx.org/headers/hx-location
type Source ¶
type Source string
Source sets the 'source' property of the HX-Location header.
More details: https://htmx.org/headers/hx-location
type Status ¶
type Status int
Status is used to set the HTTP status code of the HTMX response.
Example usage:
htmx.Response(w, htmx.Status(http.StatusGone)) // Sets the HTTP status code to 410.
const ( // StatusStopPolling sends HTTP status code 286 to the client to stop polling. // // Example usage: // htmx.Response(w, htmx.StatusStopPolling) // // Sets the HTTP status code to 286. StatusStopPolling Status = 286 )
HTMX status codes.
type Target ¶
type Target string
Target sets the 'target' property of the HX-Location header.
More details: https://htmx.org/headers/hx-location