Documentation
¶
Index ¶
- Constants
- func NewContext(req *http.Request, res http.ResponseWriter) *contextImpl
- func SetAuth[T any](ctx context.Context, auth T) context.Context
- type AuthContext
- type Context
- type EmptyResult
- type HTMX
- type LocationOptions
- type Result
- type SwapChain
- func (s *SwapChain) FocusScroll(enabled bool) *SwapChain
- func (s *SwapChain) IgnoreTitle() *SwapChain
- func (s *SwapChain) ScrollBottom(selector string) *SwapChain
- func (s *SwapChain) ScrollTop(selector string) *SwapChain
- func (s *SwapChain) ScrollWindowBottom() *SwapChain
- func (s *SwapChain) ScrollWindowTop() *SwapChain
- func (s *SwapChain) SettleDelay(d time.Duration) *SwapChain
- func (s *SwapChain) ShowBottom(selector string) *SwapChain
- func (s *SwapChain) ShowNone() *SwapChain
- func (s *SwapChain) ShowTop(selector string) *SwapChain
- func (s *SwapChain) ShowWindowBottom() *SwapChain
- func (s *SwapChain) ShowWindowTop() *SwapChain
- func (s *SwapChain) SwapDelay(d time.Duration) *SwapChain
- func (s *SwapChain) Transition() *SwapChain
- type TriggerChain
Constants ¶
const StatusStopPolling = 286
StatusStopPolling is the HTTP status code (286) that tells HTMX to stop polling when using hx-trigger="every Xs".
Variables ¶
This section is empty.
Functions ¶
func NewContext ¶
func NewContext(req *http.Request, res http.ResponseWriter) *contextImpl
NewContext creates a new request-scoped context and stores itself as a value for retrieval via ViewContext.
Types ¶
type AuthContext ¶
type AuthContext[T any] interface { Context // Auth returns the entity for the authenticated request. Auth() T }
AuthContext extends Context with access to the authenticated entity of type T.
func NewAuthContext ¶
func NewAuthContext[T any](ctx Context) AuthContext[T]
NewAuthContext wraps an existing Context into an AuthContext.
type Context ¶
type Context interface {
context.Context
// URI returns the raw request URI.
URI() string
// Header returns the value of the named request header.
Header(key string) string
// Param returns the value of a path parameter (e.g. {id}).
Param(key string) string
// Cookie returns the value of the named cookie, or "" if absent.
Cookie(name string) string
// SetCookie sets an HttpOnly cookie with the given name, value, and max age.
SetCookie(name, value string, maxAge time.Duration, secure bool)
// DelCookie deletes the named cookie by setting MaxAge to -1.
DelCookie(name string) Context
// FormFile returns the first file for the given form field name.
FormFile(name string) (multipart.File, *multipart.FileHeader, error)
// SetStatus writes the HTTP status code to the response.
SetStatus(status int)
// Redirect sends a redirect response. For HTMX requests, it sets
// the HX-Redirect header instead of a standard HTTP redirect.
Redirect(url string) Result
// IsAuthorized returns true if an auth entity has been set on the context.
IsAuthorized() bool
// HTMX returns the HTMX sub-interface for reading request headers
// and writing response headers.
HTMX() HTMX
// SetValue stores a key-value pair in the context.
SetValue(key, val any)
// WithLayouts re-enables layout rendering for this request.
WithLayouts() Context
// WithoutLayouts disables layout rendering for this request.
WithoutLayouts() Context
// IsWithoutLayouts returns true if layouts have been disabled.
IsWithoutLayouts() bool
}
Context is the request-scoped interface passed to all thx handlers. It extends context.Context with HTTP helpers for the current request.
func ViewContext ¶
ViewContext retrieves the thx Context from a standard context.Context. This is needed because templ wraps contexts with context.WithValue, making direct type assertions fail. Panics if no thx Context is found — this indicates ViewContext was called outside of a thx request lifecycle (e.g. in a cached component rendered with context.Background).
type EmptyResult ¶
type EmptyResult struct{}
EmptyResult is a Result that writes nothing to the response.
func (*EmptyResult) WriteResult ¶
func (e *EmptyResult) WriteResult(_ http.ResponseWriter) error
WriteResult is a no-op for empty results.
type HTMX ¶
type HTMX interface {
// IsRequest returns true if this is an HTMX request (HX-Request: true).
IsRequest() bool
// IsPartial returns true if this is a partial HTMX request (not boosted).
IsPartial() bool
// IsBoosted returns true if this is an HTMX boosted request.
IsBoosted() bool
// CurrentURL returns the HX-Current-URL header value.
CurrentURL() string
// IsHistoryRestoreRequest returns true if HTMX is restoring history.
IsHistoryRestoreRequest() bool
// Source returns the ID of the element that triggered the request.
Source() string
// Target returns the ID of the target element for the response.
Target() string
// Location triggers a client-side navigation via HX-Location.
Location(url string)
// LocationWithOptions triggers a client-side navigation with fine-grained options.
LocationWithOptions(opts LocationOptions) error
// PushURL pushes the given URL onto the browser's history stack.
PushURL(url string)
// PreventPushURL prevents HTMX from pushing a URL to the history stack.
PreventPushURL()
// Redirect triggers a full-page redirect via HX-Redirect.
Redirect(url string)
// Refresh triggers a full page refresh via HX-Refresh.
Refresh()
// ReplaceURL replaces the current URL in the browser's location bar.
ReplaceURL(url string)
// PreventReplaceURL prevents HTMX from replacing the current URL.
PreventReplaceURL()
// Reswap overrides the swap strategy via HX-Reswap and returns a
// SwapChain for adding modifiers like delays and scroll behavior.
Reswap(strategy string) *SwapChain
// Retarget overrides the target element via HX-Retarget.
Retarget(selector string)
// Reselect overrides which part of the response to swap via HX-Reselect.
Reselect(selector string)
// StopPolling sends HTTP 286 to stop HTMX polling.
StopPolling()
// Trigger fires a client-side event via HX-Trigger.
// Returns a TriggerChain for adding multiple events.
Trigger(event string, data any) *TriggerChain
}
HTMX provides access to HTMX request headers and response header setters.
type LocationOptions ¶
type LocationOptions struct {
Path string `json:"path"`
Source string `json:"source,omitempty"`
Event string `json:"event,omitempty"`
Handler string `json:"handler,omitempty"`
Target string `json:"target,omitempty"`
Swap string `json:"swap,omitempty"`
Values any `json:"values,omitempty"`
Headers any `json:"headers,omitempty"`
Select string `json:"select,omitempty"`
}
LocationOptions configures a client-side HTMX navigation via HX-Location.
type Result ¶
type Result interface {
WriteResult(http.ResponseWriter) error
}
Result is the interface returned by all route handlers. Implementations write the handler's response to the http.ResponseWriter.
type SwapChain ¶
type SwapChain struct {
// contains filtered or unexported fields
}
func (*SwapChain) FocusScroll ¶
FocusScroll enables or disables focus scrolling after swap.
func (*SwapChain) IgnoreTitle ¶
IgnoreTitle prevents HTMX from updating the page title.
func (*SwapChain) ScrollBottom ¶
ScrollBottom scrolls to the bottom of the element matching the selector.
func (*SwapChain) ScrollWindowBottom ¶
ScrollWindowBottom scrolls the window to the bottom.
func (*SwapChain) ScrollWindowTop ¶
ScrollWindowTop scrolls the window to the top.
func (*SwapChain) SettleDelay ¶
SettleDelay adds a settle delay modifier.
func (*SwapChain) ShowBottom ¶
ShowBottom shows the bottom of the element matching the selector in the viewport.
func (*SwapChain) ShowTop ¶
ShowTop shows the top of the element matching the selector in the viewport.
func (*SwapChain) ShowWindowBottom ¶
ShowWindowBottom scrolls the window to show the bottom.
func (*SwapChain) ShowWindowTop ¶
ShowWindowTop scrolls the window to show the top.
func (*SwapChain) Transition ¶
Transition enables view transitions for this swap.
type TriggerChain ¶
type TriggerChain struct {
// contains filtered or unexported fields
}
TriggerChain provides fluent chaining for HX-Trigger events. Each method updates the header immediately.
func (*TriggerChain) Trigger ¶
func (t *TriggerChain) Trigger(event string, data any) *TriggerChain
Trigger adds an event to the HX-Trigger header. Pass nil for data to trigger an event with no payload.