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 templ components:
<button { htmx.HxGet("/api/users")... } { htmx.HxTarget("#user-list")... } { htmx.HxSwap("innerHTML")... }>
Load Users
</button>
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:
<button { htmx.HxGet("/content")... } { htmx.HxTarget("#container")... } { htmx.HxSwap("beforeend")... }>
Load
</button>
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:
<input { htmx.HxGet("/search")... } { htmx.HxTriggerDebounce("keyup", "500ms")... } { htmx.HxTarget("#results")... }/>
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!",
},
})
Progressive Enhancement ¶
Use hx-boost for seamless navigation:
<div { htmx.HxBoost(true)... }>
<a href="/page1">Page 1</a>
<a href="/page2">Page 2</a>
</div>
Loading States ¶
Show indicators during requests:
<button { htmx.HxPost("/api/submit")... } { htmx.HxIndicator("#spinner")... } { htmx.HxDisabledElt("this")... }>
Submit
</button>
<div id="spinner" class="htmx-indicator">Loading...</div>
Extensions ¶
Load HTMX extensions for additional functionality:
@htmx.ScriptsWithExtensions(htmx.ExtensionSSE, htmx.ExtensionWebSockets)
Index ¶
- Constants
- func CloakCSS() templ.Component
- func ConfigMeta(config map[string]string) templ.Component
- func ExtensionScript(extension string) templ.Component
- 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) templ.Attributes
- func HxCloak() templ.Attributes
- func HxConfirm(message string) templ.Attributes
- func HxDelete(url string) templ.Attributes
- func HxDisabledElt(selector string) templ.Attributes
- func HxDisinherit(attributes string) templ.Attributes
- func HxEncoding(encoding string) templ.Attributes
- func HxExt(extension string) templ.Attributes
- func HxGet(url string) templ.Attributes
- func HxHeaders(headers map[string]string) templ.Attributes
- func HxHistory(enabled bool) templ.Attributes
- func HxInclude(selector string) templ.Attributes
- func HxIndicator(selector string) templ.Attributes
- func HxParams(params string) templ.Attributes
- func HxPatch(url string) templ.Attributes
- func HxPost(url string) templ.Attributes
- func HxPreserve(enabled bool) templ.Attributes
- func HxPrompt(message string) templ.Attributes
- func HxPushURL(enabled bool) templ.Attributes
- func HxPushURLWithPath(path string) templ.Attributes
- func HxPut(url string) templ.Attributes
- func HxReplaceURL(enabled bool) templ.Attributes
- func HxReplaceURLWithPath(path string) templ.Attributes
- func HxSelect(selector string) templ.Attributes
- func HxSelectOOB(selector string) templ.Attributes
- func HxSwap(strategy string) templ.Attributes
- func HxSwapAfterBegin() templ.Attributes
- func HxSwapAfterEnd() templ.Attributes
- func HxSwapBeforeBegin() templ.Attributes
- func HxSwapBeforeEnd() templ.Attributes
- func HxSwapDelete() templ.Attributes
- func HxSwapInnerHTML() templ.Attributes
- func HxSwapNone() templ.Attributes
- func HxSwapOuterHTML() templ.Attributes
- func HxSync(selector, strategy string) templ.Attributes
- func HxTarget(selector string) templ.Attributes
- func HxTrigger(event string) templ.Attributes
- func HxTriggerChange() templ.Attributes
- func HxTriggerClick() templ.Attributes
- func HxTriggerConsume(event string) templ.Attributes
- func HxTriggerDebounce(event, delay string) templ.Attributes
- func HxTriggerEvery(duration string) templ.Attributes
- func HxTriggerFilter(eventAndFilter string) templ.Attributes
- func HxTriggerFrom(eventAndSelector string) templ.Attributes
- func HxTriggerIntersect(options string) templ.Attributes
- func HxTriggerLoad() templ.Attributes
- func HxTriggerMouseEnter() templ.Attributes
- func HxTriggerMouseLeave() templ.Attributes
- func HxTriggerOnce(event string) templ.Attributes
- func HxTriggerQueue(event, queueOption string) templ.Attributes
- func HxTriggerRevealed() templ.Attributes
- func HxTriggerSubmit() templ.Attributes
- func HxTriggerTarget(event, selector string) templ.Attributes
- func HxTriggerThrottle(event, delay string) templ.Attributes
- func HxValidate(enabled bool) templ.Attributes
- func HxVals(values map[string]any) templ.Attributes
- func HxValsJS(jsExpr string) templ.Attributes
- func IndicatorCSS() templ.Component
- func IsHTMX(r *http.Request) bool
- func IsHTMXBoosted(r *http.Request) bool
- func Middleware(next http.Handler) http.Handler
- func Scripts(version ...string) templ.Component
- func ScriptsWithExtensions(extensions ...string) templ.Component
- 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 templ.Component with CSS to prevent flash of unstyled content for elements with hx-cloak attribute.
Example (in .templ files):
@htmx.CloakCSS()
func ConfigMeta ¶
ConfigMeta returns a templ.Component that renders meta tags for HTMX configuration.
Example (in .templ files):
@htmx.ConfigMeta(map[string]string{"defaultSwapStyle": "outerHTML"})
func ExtensionScript ¶
ExtensionScript returns a templ.Component for a specific HTMX extension script tag.
Example (in .templ files):
@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 ¶
func HxBoost(enabled bool) templ.Attributes
HxBoost creates an hx-boost attribute for progressive enhancement of links/forms.
Example (in .templ files):
<div { htmx.HxBoost(true)... }>
func HxCloak ¶
func HxCloak() templ.Attributes
HxCloak creates an hx-cloak attribute to hide elements until HTMX processes them.
Example (in .templ files):
<div { htmx.HxCloak()... } { htmx.HxGet("/content")... }>
func HxConfirm ¶
func HxConfirm(message string) templ.Attributes
HxConfirm creates an hx-confirm attribute for confirmation dialogs.
Example (in .templ files):
<button { htmx.HxDelete("/api/item/123")... } { htmx.HxConfirm("Are you sure?")... }>Delete</button>
func HxDelete ¶
func HxDelete(url string) templ.Attributes
HxDelete creates an hx-delete attribute for DELETE requests.
Example (in .templ files):
<button { htmx.HxDelete("/api/delete/123")... } { htmx.HxConfirm("Are you sure?")... }>Delete</button>
func HxDisabledElt ¶
func HxDisabledElt(selector string) templ.Attributes
HxDisabledElt creates an hx-disabled-elt attribute to disable elements during requests.
Example (in .templ files):
<button { htmx.HxPost("/api/submit")... } { htmx.HxDisabledElt("this")... }>Submit</button>
func HxDisinherit ¶
func HxDisinherit(attributes string) templ.Attributes
HxDisinherit creates an hx-disinherit attribute that prevents inheritance of HTMX attributes from parent elements.
Example (in .templ files):
<div { htmx.HxDisinherit("hx-select hx-get")... }>
func HxEncoding ¶
func HxEncoding(encoding string) templ.Attributes
HxEncoding creates an hx-encoding attribute to set request encoding.
Example (in .templ files):
<form { htmx.HxPost("/api/upload")... } { htmx.HxEncoding("multipart/form-data")... }>
func HxExt ¶
func HxExt(extension string) templ.Attributes
HxExt creates an hx-ext attribute to load HTMX extensions.
Example (in .templ files):
<div { htmx.HxExt("json-enc")... } { htmx.HxPost("/api/data")... }>
func HxGet ¶
func HxGet(url string) templ.Attributes
HxGet creates an hx-get attribute for GET requests.
Example (in .templ files):
<button { htmx.HxGet("/api/data")... }>Load Data</button>
func HxHeaders ¶
func HxHeaders(headers map[string]string) templ.Attributes
HxHeaders creates an hx-headers attribute for custom headers.
Example (in .templ files):
<button { htmx.HxPost("/api/data")... } { htmx.HxHeaders(map[string]string{"X-API-Key": "secret"})... }>
func HxHistory ¶
func HxHistory(enabled bool) templ.Attributes
HxHistory creates an hx-history attribute to control history behavior.
Example (in .templ files):
<div { htmx.HxHistory(false)... } { htmx.HxBoost(true)... }>
func HxInclude ¶
func HxInclude(selector string) templ.Attributes
HxInclude creates an hx-include attribute to include additional elements in requests.
Example (in .templ files):
<button { htmx.HxPost("/api/submit")... } { htmx.HxInclude("#extra-data")... }>Submit</button>
func HxIndicator ¶
func HxIndicator(selector string) templ.Attributes
HxIndicator creates an hx-indicator attribute to specify a loading indicator.
Example (in .templ files):
<button { htmx.HxGet("/api/slow")... } { htmx.HxIndicator("#spinner")... }>Load</button>
func HxParams ¶
func HxParams(params string) templ.Attributes
HxParams creates an hx-params attribute to filter parameters.
Values: "*" (all), "none", "param1,param2" (include), "not param1,param2" (exclude)
Example (in .templ files):
<form { htmx.HxPost("/api/submit")... } { htmx.HxParams("email,name")... }>
func HxPatch ¶
func HxPatch(url string) templ.Attributes
HxPatch creates an hx-patch attribute for PATCH requests.
Example (in .templ files):
<button { htmx.HxPatch("/api/partial-update/123")... }>Patch</button>
func HxPost ¶
func HxPost(url string) templ.Attributes
HxPost creates an hx-post attribute for POST requests.
Example (in .templ files):
<form { htmx.HxPost("/api/submit")... }>
func HxPreserve ¶
func HxPreserve(enabled bool) templ.Attributes
HxPreserve creates an hx-preserve attribute to preserve elements across requests.
Example (in .templ files):
<video { htmx.HxPreserve(true)... } src="/video.mp4"></video>
func HxPrompt ¶
func HxPrompt(message string) templ.Attributes
HxPrompt creates an hx-prompt attribute for user input.
Example (in .templ files):
<button { htmx.HxPost("/api/comment")... } { htmx.HxPrompt("Enter your comment")... }>Add</button>
func HxPushURL ¶
func HxPushURL(enabled bool) templ.Attributes
HxPushURL creates an hx-push-url attribute for history management.
Example (in .templ files):
<button { htmx.HxGet("/page/2")... } { htmx.HxPushURL(true)... }>Next Page</button>
func HxPushURLWithPath ¶
func HxPushURLWithPath(path string) templ.Attributes
HxPushURLWithPath creates an hx-push-url attribute with a custom path.
Example (in .templ files):
<button { htmx.HxGet("/api/page/2")... } { htmx.HxPushURLWithPath("/page/2")... }>Next</button>
func HxPut ¶
func HxPut(url string) templ.Attributes
HxPut creates an hx-put attribute for PUT requests.
Example (in .templ files):
<button { htmx.HxPut("/api/update/123")... }>Update</button>
func HxReplaceURL ¶
func HxReplaceURL(enabled bool) templ.Attributes
HxReplaceURL creates an hx-replace-url attribute to replace the current URL.
Example (in .templ files):
<button { htmx.HxGet("/search?q=test")... } { htmx.HxReplaceURL(true)... }>Search</button>
func HxReplaceURLWithPath ¶
func HxReplaceURLWithPath(path string) templ.Attributes
HxReplaceURLWithPath creates an hx-replace-url attribute with a custom path.
func HxSelect ¶
func HxSelect(selector string) templ.Attributes
HxSelect creates an hx-select attribute for response filtering.
Example (in .templ files):
<button { htmx.HxGet("/full-page")... } { htmx.HxSelect("#content")... }>Load Content</button>
func HxSelectOOB ¶
func HxSelectOOB(selector string) templ.Attributes
HxSelectOOB creates an hx-select-oob attribute for out-of-band swapping.
Example:
htmx.HxSelectOOB("#notifications, #messages")
func HxSwap ¶
func HxSwap(strategy string) templ.Attributes
HxSwap creates an hx-swap attribute with a swap strategy.
Strategies: innerHTML, outerHTML, beforebegin, afterbegin, beforeend, afterend, delete, none
Example (in .templ files):
<button { htmx.HxGet("/api/item")... } { htmx.HxSwap("outerHTML")... }>Replace</button>
func HxSwapAfterBegin ¶
func HxSwapAfterBegin() templ.Attributes
HxSwapAfterBegin creates an hx-swap="afterbegin" attribute.
func HxSwapAfterEnd ¶
func HxSwapAfterEnd() templ.Attributes
HxSwapAfterEnd creates an hx-swap="afterend" attribute.
func HxSwapBeforeBegin ¶
func HxSwapBeforeBegin() templ.Attributes
HxSwapBeforeBegin creates an hx-swap="beforebegin" attribute.
func HxSwapBeforeEnd ¶
func HxSwapBeforeEnd() templ.Attributes
HxSwapBeforeEnd creates an hx-swap="beforeend" attribute.
func HxSwapDelete ¶
func HxSwapDelete() templ.Attributes
HxSwapDelete creates an hx-swap="delete" attribute.
func HxSwapInnerHTML ¶
func HxSwapInnerHTML() templ.Attributes
HxSwapInnerHTML creates an hx-swap="innerHTML" attribute.
func HxSwapNone ¶
func HxSwapNone() templ.Attributes
HxSwapNone creates an hx-swap="none" attribute.
func HxSwapOuterHTML ¶
func HxSwapOuterHTML() templ.Attributes
HxSwapOuterHTML creates an hx-swap="outerHTML" attribute.
func HxSync ¶
func HxSync(selector, strategy string) templ.Attributes
HxSync creates an hx-sync attribute for request synchronization.
Strategies: drop, abort, replace, queue, queue first, queue last, queue all
Example (in .templ files):
<input { htmx.HxGet("/search")... } { htmx.HxSync("this", "replace")... } type="text"/>
func HxTarget ¶
func HxTarget(selector string) templ.Attributes
HxTarget creates an hx-target attribute to specify where to swap content.
Example (in .templ files):
<button { htmx.HxGet("/api/data")... } { htmx.HxTarget("#results")... }>Load</button>
func HxTrigger ¶
func HxTrigger(event string) templ.Attributes
HxTrigger creates an hx-trigger attribute with a custom event.
Example (in .templ files):
<div { htmx.HxTrigger("click")... } { htmx.HxGet("/api/data")... }>
func HxTriggerChange ¶
func HxTriggerChange() templ.Attributes
HxTriggerChange creates an hx-trigger="change" attribute.
func HxTriggerClick ¶
func HxTriggerClick() templ.Attributes
HxTriggerClick creates an hx-trigger="click" attribute.
func HxTriggerConsume ¶
func HxTriggerConsume(event string) templ.Attributes
HxTriggerConsume creates an hx-trigger with consume modifier. Prevents the event from bubbling.
Example (in .templ files):
<button { htmx.HxTriggerConsume("click")... } { htmx.HxPost("/api/action")... }>
func HxTriggerDebounce ¶
func HxTriggerDebounce(event, delay string) templ.Attributes
HxTriggerDebounce creates an hx-trigger with debouncing.
Example (in .templ files):
<input { htmx.HxTriggerDebounce("keyup", "500ms")... } { htmx.HxGet("/search")... }/>
func HxTriggerEvery ¶
func HxTriggerEvery(duration string) templ.Attributes
HxTriggerEvery creates an hx-trigger with polling interval.
Example (in .templ files):
<div { htmx.HxTriggerEvery("2s")... } { htmx.HxGet("/api/status")... }>
func HxTriggerFilter ¶
func HxTriggerFilter(eventAndFilter string) templ.Attributes
HxTriggerFilter creates an hx-trigger with an event filter.
Example (in .templ files):
<input { htmx.HxTriggerFilter("keyup[key=='Enter']")... } { htmx.HxPost("/api/submit")... }/>
func HxTriggerFrom ¶
func HxTriggerFrom(eventAndSelector string) templ.Attributes
HxTriggerFrom creates an hx-trigger from another element.
Example (in .templ files):
<div id="target" { htmx.HxTriggerFrom("click from:#button")... } { htmx.HxGet("/api/data")... }>
func HxTriggerIntersect ¶
func HxTriggerIntersect(options string) templ.Attributes
HxTriggerIntersect creates an hx-trigger="intersect" attribute. Triggers when the element intersects the viewport.
Example (in .templ files):
<div { htmx.HxTriggerIntersect("once threshold:0.5")... } { htmx.HxGet("/api/lazy-load")... }>
func HxTriggerLoad ¶
func HxTriggerLoad() templ.Attributes
HxTriggerLoad creates an hx-trigger="load" attribute. Triggers when the element is first loaded.
func HxTriggerMouseEnter ¶
func HxTriggerMouseEnter() templ.Attributes
HxTriggerMouseEnter creates an hx-trigger="mouseenter" attribute.
func HxTriggerMouseLeave ¶
func HxTriggerMouseLeave() templ.Attributes
HxTriggerMouseLeave creates an hx-trigger="mouseleave" attribute.
func HxTriggerOnce ¶
func HxTriggerOnce(event string) templ.Attributes
HxTriggerOnce creates an hx-trigger that fires only once.
Example (in .templ files):
<div { htmx.HxTriggerOnce("click")... } { htmx.HxGet("/api/init")... }>
func HxTriggerQueue ¶
func HxTriggerQueue(event, queueOption string) templ.Attributes
HxTriggerQueue creates an hx-trigger with queue modifier.
Example (in .templ files):
<button { htmx.HxTriggerQueue("click", "first")... } { htmx.HxPost("/api/action")... }>
func HxTriggerRevealed ¶
func HxTriggerRevealed() templ.Attributes
HxTriggerRevealed creates an hx-trigger="revealed" attribute. Triggers when the element is scrolled into the viewport.
func HxTriggerSubmit ¶
func HxTriggerSubmit() templ.Attributes
HxTriggerSubmit creates an hx-trigger="submit" attribute.
func HxTriggerTarget ¶
func HxTriggerTarget(event, selector string) templ.Attributes
HxTriggerTarget creates an hx-trigger with target modifier.
Example (in .templ files):
<div { htmx.HxTriggerTarget("click", "#button")... } { htmx.HxGet("/api/data")... }>
func HxTriggerThrottle ¶
func HxTriggerThrottle(event, delay string) templ.Attributes
HxTriggerThrottle creates an hx-trigger with throttling.
Example (in .templ files):
<input { htmx.HxTriggerThrottle("keyup", "1s")... } { htmx.HxGet("/search")... }/>
func HxValidate ¶
func HxValidate(enabled bool) templ.Attributes
HxValidate creates an hx-validate attribute to force validation before submit.
Example (in .templ files):
<input type="email" required { htmx.HxValidate(true)... }/>
func HxVals ¶
func HxVals(values map[string]any) templ.Attributes
HxVals creates an hx-vals attribute for extra values to submit.
Example (in .templ files):
<button { htmx.HxPost("/api/data")... } { htmx.HxVals(map[string]any{"category": "urgent"})... }>
func HxValsJS ¶
func HxValsJS(jsExpr string) templ.Attributes
HxValsJS creates an hx-vals attribute with JavaScript evaluation.
Example:
htmx.HxValsJS("js:{timestamp: Date.now()}")
func IndicatorCSS ¶
IndicatorCSS returns a templ.Component with default HTMX indicator styles.
Example (in .templ files):
@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 templ.Component that renders the HTMX script tag from CDN.
Example (in .templ files):
@htmx.Scripts()
func ScriptsWithExtensions ¶
ScriptsWithExtensions returns a templ.Component that loads HTMX with the specified extensions.
Example (in .templ files):
@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.