Documentation
¶
Overview ¶
Package htmx provides HTMX integration for ForgeUI.
HTMX allows you to access AJAX, CSS Transitions, WebSockets and Server Sent Events directly in HTML, using attributes. This enables building modern, interactive user interfaces while keeping your application architecture simple and maintainable.
Basic Usage ¶
Add HTMX attributes to your HTML elements:
html.Button(
htmx.HxGet("/api/users"),
htmx.HxTarget("#user-list"),
htmx.HxSwap("innerHTML"),
g.Text("Load Users"),
)
HTTP Methods ¶
HTMX supports all standard HTTP methods:
htmx.HxGet("/api/data") // GET request
htmx.HxPost("/api/create") // POST request
htmx.HxPut("/api/update/1") // PUT request
htmx.HxPatch("/api/patch/1") // PATCH request
htmx.HxDelete("/api/del/1") // DELETE request
Targeting and Swapping ¶
Control where and how responses are inserted:
html.Button(
htmx.HxGet("/content"),
htmx.HxTarget("#container"),
htmx.HxSwap("beforeend"), // Append to container
)
Available swap strategies:
- innerHTML (default): Replace inner HTML
- outerHTML: Replace entire element
- beforebegin: Insert before element
- afterbegin: Insert as first child
- beforeend: Insert as last child
- afterend: Insert after element
- delete: Delete the target
- none: Don't swap
Triggers ¶
Specify when requests are made:
html.Input(
htmx.HxGet("/search"),
htmx.HxTriggerDebounce("keyup", "500ms"),
htmx.HxTarget("#results"),
)
Common triggers:
- click: On element click
- change: On input change
- submit: On form submit
- load: When element loads
- revealed: When scrolled into view
- intersect: When element intersects viewport
Server-Side Usage ¶
Detect HTMX requests and respond appropriately:
func handler(w http.ResponseWriter, r *http.Request) {
if htmx.IsHTMX(r) {
// Return partial HTML
renderPartial(w, data)
} else {
// Return full page
renderFullPage(w, data)
}
}
Response Headers ¶
Control client behavior with response headers:
htmx.SetHTMXTrigger(w, map[string]any{
"showMessage": map[string]string{
"level": "success",
"text": "Item saved!",
},
})
Available response headers:
- HX-Trigger: Trigger client-side events
- HX-Redirect: Perform client-side redirect
- HX-Refresh: Force full page refresh
- HX-Retarget: Change swap target
- HX-Reswap: Change swap strategy
- HX-Push-Url: Update browser URL
- HX-Replace-Url: Replace browser URL
Progressive Enhancement ¶
Use hx-boost for seamless navigation:
html.Div(
htmx.HxBoost(true),
html.A(html.Href("/page1"), g.Text("Page 1")),
html.A(html.Href("/page2"), g.Text("Page 2")),
)
Loading States ¶
Show indicators during requests:
html.Button(
htmx.HxPost("/api/submit"),
htmx.HxIndicator("#spinner"),
htmx.HxDisabledElt("this"),
g.Text("Submit"),
)
html.Div(
html.ID("spinner"),
html.Class("htmx-indicator"),
g.Text("Loading..."),
)
Extensions ¶
Load HTMX extensions for additional functionality:
htmx.ScriptsWithExtensions(
htmx.ExtensionSSE,
htmx.ExtensionWebSockets,
)
Index ¶
- Constants
- func CloakCSS() g.Node
- func ConfigMeta(config map[string]string) []g.Node
- func ExtensionScript(extension string) g.Node
- func HTMXCurrentURL(r *http.Request) string
- func HTMXHistoryRestoreRequest(r *http.Request) bool
- func HTMXPrompt(r *http.Request) string
- func HTMXTarget(r *http.Request) string
- func HTMXTrigger(r *http.Request) string
- func HTMXTriggerName(r *http.Request) string
- func HxBoost(enabled bool) g.Node
- func HxCloak() g.Node
- func HxConfirm(message string) g.Node
- func HxDelete(url string) g.Node
- func HxDisabledElt(selector string) g.Node
- func HxDisinherit(attributes string) g.Node
- func HxEncoding(encoding string) g.Node
- func HxExt(extension string) g.Node
- func HxGet(url string) g.Node
- func HxHeaders(headers map[string]string) g.Node
- func HxHistory(enabled bool) g.Node
- func HxInclude(selector string) g.Node
- func HxIndicator(selector string) g.Node
- func HxParams(params string) g.Node
- func HxPatch(url string) g.Node
- func HxPost(url string) g.Node
- func HxPreserve(enabled bool) g.Node
- func HxPrompt(message string) g.Node
- func HxPushURL(enabled bool) g.Node
- func HxPushURLWithPath(path string) g.Node
- func HxPut(url string) g.Node
- func HxReplaceURL(enabled bool) g.Node
- func HxReplaceURLWithPath(path string) g.Node
- func HxSelect(selector string) g.Node
- func HxSelectOOB(selector string) g.Node
- func HxSwap(strategy string) g.Node
- func HxSwapAfterBegin() g.Node
- func HxSwapAfterEnd() g.Node
- func HxSwapBeforeBegin() g.Node
- func HxSwapBeforeEnd() g.Node
- func HxSwapDelete() g.Node
- func HxSwapInnerHTML() g.Node
- func HxSwapNone() g.Node
- func HxSwapOuterHTML() g.Node
- func HxSync(selector, strategy string) g.Node
- func HxTarget(selector string) g.Node
- func HxTrigger(event string) g.Node
- func HxTriggerChange() g.Node
- func HxTriggerClick() g.Node
- func HxTriggerConsume(event string) g.Node
- func HxTriggerDebounce(event, delay string) g.Node
- func HxTriggerEvery(duration string) g.Node
- func HxTriggerFilter(eventAndFilter string) g.Node
- func HxTriggerFrom(eventAndSelector string) g.Node
- func HxTriggerIntersect(options string) g.Node
- func HxTriggerLoad() g.Node
- func HxTriggerMouseEnter() g.Node
- func HxTriggerMouseLeave() g.Node
- func HxTriggerOnce(event string) g.Node
- func HxTriggerQueue(event, queueOption string) g.Node
- func HxTriggerRevealed() g.Node
- func HxTriggerSubmit() g.Node
- func HxTriggerTarget(event, selector string) g.Node
- func HxTriggerThrottle(event, delay string) g.Node
- func HxValidate(enabled bool) g.Node
- func HxVals(values map[string]any) g.Node
- func HxValsJS(jsExpr string) g.Node
- func IndicatorCSS() g.Node
- func IsHTMX(r *http.Request) bool
- func IsHTMXBoosted(r *http.Request) bool
- func Middleware(next http.Handler) http.Handler
- func Scripts(version ...string) g.Node
- func ScriptsWithExtensions(extensions ...string) []g.Node
- func SetHTMXLocation(w http.ResponseWriter, path string)
- func SetHTMXLocationWithContext(w http.ResponseWriter, context map[string]any)
- func SetHTMXPushURL(w http.ResponseWriter, url string)
- func SetHTMXRedirect(w http.ResponseWriter, url string)
- func SetHTMXRefresh(w http.ResponseWriter)
- func SetHTMXReplaceURL(w http.ResponseWriter, url string)
- func SetHTMXReselect(w http.ResponseWriter, selector string)
- func SetHTMXReswap(w http.ResponseWriter, swapMethod string)
- func SetHTMXRetarget(w http.ResponseWriter, target string)
- func SetHTMXTrigger(w http.ResponseWriter, events map[string]any)
- func SetResponseHeaders(w http.ResponseWriter, headers ResponseHeaders)
- func StopPolling(w http.ResponseWriter)
- func TriggerEvent(w http.ResponseWriter, eventName string)
- func TriggerEventWithDetail(w http.ResponseWriter, eventName string, detail map[string]any)
- func TriggerEvents(w http.ResponseWriter, events map[string]any)
- type ResponseHeaders
Constants ¶
const ( ExtensionSSE = "sse" ExtensionWebSockets = "ws" ExtensionClassTools = "class-tools" ExtensionPreload = "preload" ExtensionHeadSupport = "head-support" ExtensionResponseTargets = "response-targets" ExtensionDebug = "debug" ExtensionEventHeader = "event-header" ExtensionIncludeVals = "include-vals" ExtensionJSONEnc = "json-enc" ExtensionMethodOverride = "method-override" ExtensionMorphdom = "morphdom-swap" ExtensionMultiSwap = "multi-swap" ExtensionPathDeps = "path-deps" ExtensionRestoreOnError = "restored" )
HTMX extension names
const DefaultVersion = "2.0.3"
DefaultVersion is the default HTMX version.
Variables ¶
This section is empty.
Functions ¶
func CloakCSS ¶
CloakCSS returns a style tag that prevents flash of unstyled content for elements with hx-cloak attribute.
Add this to your page head and use HxCloak() on elements that should be hidden until HTMX initializes.
Example:
html.Head(
htmx.CloakCSS(),
)
html.Body(
html.Div(
htmx.HxCloak(),
htmx.HxGet("/content"),
htmx.HxTriggerLoad(),
),
)
func ConfigMeta ¶
ConfigMeta returns meta tags for HTMX configuration.
Example:
html.Head(
htmx.ConfigMeta(map[string]string{
"defaultSwapStyle": "outerHTML",
"timeout": "5000",
}),
)
func ExtensionScript ¶
ExtensionScript returns a script tag for a specific HTMX extension.
Example:
html.Head(
htmx.Scripts(),
htmx.ExtensionScript(htmx.ExtensionSSE),
)
func HTMXCurrentURL ¶
HTMXCurrentURL gets the current URL of the browser when the HTMX request was made.
Example:
currentURL := htmx.HTMXCurrentURL(r)
func HTMXHistoryRestoreRequest ¶
HTMXHistoryRestoreRequest checks if this request is for history restoration after a miss in the local history cache.
Example:
if htmx.HTMXHistoryRestoreRequest(r) {
// Handle history restoration
}
func HTMXPrompt ¶
HTMXPrompt gets the user response to an hx-prompt.
Example:
if prompt := htmx.HTMXPrompt(r); prompt != "" {
// Use the prompt value
processComment(prompt)
}
func HTMXTarget ¶
HTMXTarget gets the ID of the target element if it exists.
Example:
targetID := htmx.HTMXTarget(r)
func HTMXTrigger ¶
HTMXTrigger gets the ID of the triggered element if it exists.
Example:
triggerID := htmx.HTMXTrigger(r)
func HTMXTriggerName ¶
HTMXTriggerName gets the name of the triggered element if it exists.
Example:
triggerName := htmx.HTMXTriggerName(r)
func HxBoost ¶
HxBoost creates an hx-boost attribute for progressive enhancement of links/forms.
Example:
html.Div(
htmx.HxBoost(true),
html.A(html.Href("/page1"), g.Text("Page 1")),
html.A(html.Href("/page2"), g.Text("Page 2")),
)
func HxCloak ¶
HxCloak creates an hx-cloak attribute to hide elements until HTMX processes them.
Example:
html.Div(
htmx.HxCloak(),
htmx.HxGet("/content"),
htmx.HxTriggerLoad(),
)
func HxConfirm ¶
HxConfirm creates an hx-confirm attribute for confirmation dialogs.
Example:
html.Button(
htmx.HxDelete("/api/item/123"),
htmx.HxConfirm("Are you sure you want to delete this item?"),
g.Text("Delete"),
)
func HxDelete ¶
HxDelete creates an hx-delete attribute for DELETE requests.
Example:
html.Button(
htmx.HxDelete("/api/delete/123"),
htmx.HxConfirm("Are you sure?"),
g.Text("Delete"),
)
func HxDisabledElt ¶
HxDisabledElt creates an hx-disabled-elt attribute to disable elements during requests.
Example:
html.Button(
htmx.HxPost("/api/submit"),
htmx.HxDisabledElt("this"),
g.Text("Submit"),
)
func HxDisinherit ¶
HxDisinherit creates an hx-disinherit attribute that prevents inheritance of HTMX attributes from parent elements.
Example:
html.Div(
htmx.HxDisinherit("hx-select hx-get"),
// child elements won't inherit hx-select or hx-get
)
func HxEncoding ¶
HxEncoding creates an hx-encoding attribute to set request encoding.
Example:
html.Form(
htmx.HxPost("/api/upload"),
htmx.HxEncoding("multipart/form-data"),
// file input...
)
func HxExt ¶
HxExt creates an hx-ext attribute to load HTMX extensions.
Example:
html.Div(
htmx.HxExt("json-enc"),
htmx.HxPost("/api/data"),
)
func HxGet ¶
HxGet creates an hx-get attribute for GET requests.
Example:
html.Button(
htmx.HxGet("/api/data"),
g.Text("Load Data"),
)
func HxHeaders ¶
HxHeaders creates an hx-headers attribute for custom headers.
Example:
html.Button(
htmx.HxPost("/api/data"),
htmx.HxHeaders(map[string]string{
"X-API-Key": "secret",
}),
g.Text("Submit"),
)
func HxHistory ¶
HxHistory creates an hx-history attribute to control history behavior.
Example:
html.Div(
htmx.HxHistory(false),
htmx.HxBoost(true),
// links won't be added to history
)
func HxInclude ¶
HxInclude creates an hx-include attribute to include additional elements in requests.
Example:
html.Button(
htmx.HxPost("/api/submit"),
htmx.HxInclude("#extra-data"),
g.Text("Submit"),
)
func HxIndicator ¶
HxIndicator creates an hx-indicator attribute to specify a loading indicator.
Example:
html.Button(
htmx.HxGet("/api/slow"),
htmx.HxIndicator("#spinner"),
g.Text("Load"),
)
html.Div(
html.ID("spinner"),
html.Class("htmx-indicator"),
g.Text("Loading..."),
)
func HxParams ¶
HxParams creates an hx-params attribute to filter parameters.
Values:
- "*": Include all parameters (default)
- "none": Include no parameters
- "param1,param2": Include only specified parameters
- "not param1,param2": Include all except specified parameters
Example:
html.Form(
htmx.HxPost("/api/submit"),
htmx.HxParams("email,name"),
// form fields...
)
func HxPatch ¶
HxPatch creates an hx-patch attribute for PATCH requests.
Example:
html.Button(
htmx.HxPatch("/api/partial-update/123"),
g.Text("Patch"),
)
func HxPost ¶
HxPost creates an hx-post attribute for POST requests.
Example:
html.Form(
htmx.HxPost("/api/submit"),
// form fields...
)
func HxPreserve ¶
HxPreserve creates an hx-preserve attribute to preserve elements across requests.
Example:
html.Video(
htmx.HxPreserve(true),
html.Src("/video.mp4"),
)
func HxPrompt ¶
HxPrompt creates an hx-prompt attribute for user input.
Example:
html.Button(
htmx.HxPost("/api/comment"),
htmx.HxPrompt("Enter your comment"),
g.Text("Add Comment"),
)
func HxPushURL ¶
HxPushURL creates an hx-push-url attribute for history management.
Example:
html.Button(
htmx.HxGet("/page/2"),
htmx.HxPushURL(true),
g.Text("Next Page"),
)
func HxPushURLWithPath ¶
HxPushURLWithPath creates an hx-push-url attribute with a custom path.
Example:
html.Button(
htmx.HxGet("/api/page/2"),
htmx.HxPushURLWithPath("/page/2"),
g.Text("Next Page"),
)
func HxPut ¶
HxPut creates an hx-put attribute for PUT requests.
Example:
html.Button(
htmx.HxPut("/api/update/123"),
g.Text("Update"),
)
func HxReplaceURL ¶
HxReplaceURL creates an hx-replace-url attribute to replace the current URL.
Example:
html.Button(
htmx.HxGet("/search?q=test"),
htmx.HxReplaceURL(true),
g.Text("Search"),
)
func HxReplaceURLWithPath ¶
HxReplaceURLWithPath creates an hx-replace-url attribute with a custom path.
func HxSelect ¶
HxSelect creates an hx-select attribute for response filtering.
Example:
html.Button(
htmx.HxGet("/full-page"),
htmx.HxSelect("#content"),
htmx.HxTarget("#main"),
g.Text("Load Content"),
)
func HxSelectOOB ¶
HxSelectOOB creates an hx-select-oob attribute for out-of-band swapping.
Example:
htmx.HxSelectOOB("#notifications, #messages")
func HxSwap ¶
HxSwap creates an hx-swap attribute with a swap strategy.
Strategies:
- innerHTML (default): Replace the inner html of the target element
- outerHTML: Replace the entire target element
- beforebegin: Insert before the target element
- afterbegin: Insert before the first child of the target element
- beforeend: Insert after the last child of the target element
- afterend: Insert after the target element
- delete: Deletes the target element regardless of the response
- none: Does not append content
Example:
html.Button(
htmx.HxGet("/api/item"),
htmx.HxSwap("outerHTML"),
g.Text("Replace"),
)
func HxSwapAfterBegin ¶
HxSwapAfterBegin creates an hx-swap="afterbegin" attribute.
func HxSwapAfterEnd ¶
HxSwapAfterEnd creates an hx-swap="afterend" attribute.
func HxSwapBeforeBegin ¶
HxSwapBeforeBegin creates an hx-swap="beforebegin" attribute.
func HxSwapBeforeEnd ¶
HxSwapBeforeEnd creates an hx-swap="beforeend" attribute.
func HxSwapInnerHTML ¶
HxSwapInnerHTML creates an hx-swap="innerHTML" attribute.
func HxSwapOuterHTML ¶
HxSwapOuterHTML creates an hx-swap="outerHTML" attribute.
func HxSync ¶
HxSync creates an hx-sync attribute for request synchronization.
Strategies:
- drop: Drop (ignore) the request if another is in flight
- abort: Abort the current request if a new one is triggered
- replace: Replace the current request if a new one is triggered
- queue: Queue requests
- queue first: Queue requests, but execute the first immediately
- queue last: Queue requests, but execute the last immediately
- queue all: Queue all requests
Example:
html.Input(
htmx.HxGet("/search"),
htmx.HxSync("this", "replace"),
html.Type("text"),
)
func HxTarget ¶
HxTarget creates an hx-target attribute to specify where to swap content.
Example:
html.Button(
htmx.HxGet("/api/data"),
htmx.HxTarget("#results"),
g.Text("Load"),
)
func HxTrigger ¶
HxTrigger creates an hx-trigger attribute with a custom event.
Example:
html.Div(
htmx.HxTrigger("click"),
htmx.HxGet("/api/data"),
)
func HxTriggerChange ¶
HxTriggerChange creates an hx-trigger="change" attribute.
func HxTriggerClick ¶
HxTriggerClick creates an hx-trigger="click" attribute.
func HxTriggerConsume ¶
HxTriggerConsume creates an hx-trigger with consume modifier. Prevents the event from bubbling.
Example:
html.Button(
htmx.HxTriggerConsume("click"),
htmx.HxPost("/api/action"),
)
func HxTriggerDebounce ¶
HxTriggerDebounce creates an hx-trigger with debouncing.
Example:
html.Input(
htmx.HxTriggerDebounce("keyup", "500ms"),
htmx.HxGet("/search"),
)
func HxTriggerEvery ¶
HxTriggerEvery creates an hx-trigger with polling interval.
Example:
html.Div(
htmx.HxTriggerEvery("2s"),
htmx.HxGet("/api/status"),
)
func HxTriggerFilter ¶
HxTriggerFilter creates an hx-trigger with an event filter.
Example:
html.Input(
htmx.HxTriggerFilter("keyup[key=='Enter']"),
htmx.HxPost("/api/submit"),
)
func HxTriggerFrom ¶
HxTriggerFrom creates an hx-trigger from another element.
Example:
html.Div(
html.ID("target"),
htmx.HxTriggerFrom("click from:#button"),
htmx.HxGet("/api/data"),
)
func HxTriggerIntersect ¶
HxTriggerIntersect creates an hx-trigger="intersect" attribute. Triggers when the element intersects the viewport.
Example:
html.Div(
htmx.HxTriggerIntersect("once threshold:0.5"),
htmx.HxGet("/api/lazy-load"),
)
func HxTriggerLoad ¶
HxTriggerLoad creates an hx-trigger="load" attribute. Triggers when the element is first loaded.
func HxTriggerMouseEnter ¶
HxTriggerMouseEnter creates an hx-trigger="mouseenter" attribute.
func HxTriggerMouseLeave ¶
HxTriggerMouseLeave creates an hx-trigger="mouseleave" attribute.
func HxTriggerOnce ¶
HxTriggerOnce creates an hx-trigger that fires only once.
Example:
html.Div(
htmx.HxTriggerOnce("click"),
htmx.HxGet("/api/init"),
)
func HxTriggerQueue ¶
HxTriggerQueue creates an hx-trigger with queue modifier.
Example:
html.Button(
htmx.HxTriggerQueue("click", "first"),
htmx.HxPost("/api/action"),
)
func HxTriggerRevealed ¶
HxTriggerRevealed creates an hx-trigger="revealed" attribute. Triggers when the element is scrolled into the viewport.
func HxTriggerSubmit ¶
HxTriggerSubmit creates an hx-trigger="submit" attribute.
func HxTriggerTarget ¶
HxTriggerTarget creates an hx-trigger with target modifier.
Example:
html.Div(
htmx.HxTriggerTarget("click", "#button"),
htmx.HxGet("/api/data"),
)
func HxTriggerThrottle ¶
HxTriggerThrottle creates an hx-trigger with throttling.
Example:
html.Input(
htmx.HxTriggerThrottle("keyup", "1s"),
htmx.HxGet("/search"),
)
func HxValidate ¶
HxValidate creates an hx-validate attribute to force validation before submit.
Example:
html.Input(
html.Type("email"),
html.Required(),
htmx.HxValidate(true),
)
func HxVals ¶
HxVals creates an hx-vals attribute for extra values to submit.
Example:
html.Button(
htmx.HxPost("/api/data"),
htmx.HxVals(map[string]any{
"category": "urgent",
"priority": 1,
}),
g.Text("Submit"),
)
func HxValsJS ¶
HxValsJS creates an hx-vals attribute with JavaScript evaluation.
Example:
htmx.HxValsJS("js:{timestamp: Date.now()}")
func IndicatorCSS ¶
IndicatorCSS returns a style tag with default HTMX indicator styles.
Elements with class .htmx-indicator will be hidden by default and shown during HTMX requests when targeted by hx-indicator.
Example:
html.Head(
htmx.IndicatorCSS(),
)
func IsHTMX ¶
IsHTMX checks if the request is from HTMX by looking for the HX-Request header.
Example:
func handler(w http.ResponseWriter, r *http.Request) {
if htmx.IsHTMX(r) {
// Return partial HTML
renderPartial(w)
} else {
// Return full page
renderFullPage(w)
}
}
func IsHTMXBoosted ¶
IsHTMXBoosted checks if the request is from an element using hx-boost.
Example:
func handler(w http.ResponseWriter, r *http.Request) {
if htmx.IsHTMXBoosted(r) {
// Handle boosted navigation
}
}
func Middleware ¶
Middleware creates an HTTP middleware that detects HTMX requests. It adds HTMX request information to the request context.
Example:
router := http.NewServeMux()
router.Handle("/", htmx.Middleware(handler))
func Scripts ¶
Scripts returns a script tag that loads HTMX from CDN.
Example:
html.Head(
htmx.Scripts(),
)
func ScriptsWithExtensions ¶
ScriptsWithExtensions loads HTMX with the specified extensions.
Example:
html.Head(
g.Group(htmx.ScriptsWithExtensions(
htmx.ExtensionSSE,
htmx.ExtensionWebSockets,
)),
)
func SetHTMXLocation ¶
func SetHTMXLocation(w http.ResponseWriter, path string)
SetHTMXLocation performs a client-side redirect without a full page reload.
Example:
htmx.SetHTMXLocation(w, "/new-page")
func SetHTMXLocationWithContext ¶
func SetHTMXLocationWithContext(w http.ResponseWriter, context map[string]any)
SetHTMXLocationWithContext performs a client-side redirect with context.
Example:
htmx.SetHTMXLocationWithContext(w, map[string]any{
"path": "/messages",
"target": "#main",
"swap": "innerHTML",
})
func SetHTMXPushURL ¶
func SetHTMXPushURL(w http.ResponseWriter, url string)
SetHTMXPushURL pushes a new URL into the browser history stack.
Example:
htmx.SetHTMXPushURL(w, "/page/2")
func SetHTMXRedirect ¶
func SetHTMXRedirect(w http.ResponseWriter, url string)
SetHTMXRedirect performs a client-side redirect that does a full page reload.
Example:
htmx.SetHTMXRedirect(w, "/login")
func SetHTMXRefresh ¶
func SetHTMXRefresh(w http.ResponseWriter)
SetHTMXRefresh tells HTMX to do a full page refresh.
Example:
htmx.SetHTMXRefresh(w)
func SetHTMXReplaceURL ¶
func SetHTMXReplaceURL(w http.ResponseWriter, url string)
SetHTMXReplaceURL replaces the current URL in the location bar.
Example:
htmx.SetHTMXReplaceURL(w, "/new-url")
func SetHTMXReselect ¶
func SetHTMXReselect(w http.ResponseWriter, selector string)
SetHTMXReselect allows you to select a subset of the response to swap.
Example:
htmx.SetHTMXReselect(w, "#content")
func SetHTMXReswap ¶
func SetHTMXReswap(w http.ResponseWriter, swapMethod string)
SetHTMXReswap allows you to specify how the response will be swapped.
Example:
htmx.SetHTMXReswap(w, "outerHTML")
func SetHTMXRetarget ¶
func SetHTMXRetarget(w http.ResponseWriter, target string)
SetHTMXRetarget allows you to specify a new target for the swap.
Example:
htmx.SetHTMXRetarget(w, "#different-target")
func SetHTMXTrigger ¶
func SetHTMXTrigger(w http.ResponseWriter, events map[string]any)
SetHTMXTrigger is a convenience function to set the HX-Trigger response header.
Example:
htmx.SetHTMXTrigger(w, map[string]any{
"itemUpdated": map[string]int{"id": 123},
})
func SetResponseHeaders ¶
func SetResponseHeaders(w http.ResponseWriter, headers ResponseHeaders)
SetResponseHeaders sets HTMX response headers.
Example:
htmx.SetResponseHeaders(w, htmx.ResponseHeaders{
Trigger: map[string]any{
"showMessage": map[string]string{"text": "Saved!"},
},
Refresh: false,
})
func StopPolling ¶
func StopPolling(w http.ResponseWriter)
StopPolling sets HX-Trigger with status code 286 to stop polling.
Example:
if completed {
htmx.StopPolling(w)
return
}
func TriggerEvent ¶
func TriggerEvent(w http.ResponseWriter, eventName string)
TriggerEvent sets an HX-Trigger response header with a simple event.
Example:
htmx.TriggerEvent(w, "showMessage")
func TriggerEventWithDetail ¶
func TriggerEventWithDetail(w http.ResponseWriter, eventName string, detail map[string]any)
TriggerEventWithDetail sets an HX-Trigger response header with event details.
Example:
htmx.TriggerEventWithDetail(w, "showMessage", map[string]any{
"level": "success",
"message": "Item saved successfully",
})
func TriggerEvents ¶
func TriggerEvents(w http.ResponseWriter, events map[string]any)
TriggerEvents sets multiple HX-Trigger events.
Example:
htmx.TriggerEvents(w, map[string]any{
"event1": nil,
"event2": map[string]string{"key": "value"},
})
Types ¶
type ResponseHeaders ¶
type ResponseHeaders struct {
// Trigger allows you to trigger client-side events
Trigger map[string]any
// TriggerAfterSwap triggers events after the swap step
TriggerAfterSwap map[string]any
// TriggerAfterSettle triggers events after the settle step
TriggerAfterSettle map[string]any
// Redirect performs a client-side redirect
Redirect string
// Refresh forces a full page refresh
Refresh bool
// ReplaceURL replaces the current URL in the browser location bar
ReplaceURL string
// PushURL pushes a new URL into the browser history
PushURL string
// Reswap allows you to specify how the response will be swapped
Reswap string
// Retarget allows you to specify a new target for the swap
Retarget string
// Reselect allows you to select a subset of the response to swap
Reselect string
}
ResponseHeaders contains HTMX response headers.