Documentation ¶
Overview ¶
Package htmx offers a streamlined integration with HTMX in Go applications. It implements the standard io.Writer interface and includes middleware support, but it is not required. Allowing for the effortless incorporation of HTMX features into existing Go applications.
Index ¶
- Constants
- Variables
- func HxBoolToStr(b bool) string
- func HxStrToBool(str string) bool
- func IsHxBoosted(r *http.Request) bool
- func IsHxHistoryRestoreRequest(r *http.Request) bool
- func IsHxRequest(r *http.Request) bool
- func RenderPartial(r *http.Request) bool
- type Component
- func (c *Component) AddData(key string, value any) RenderableComponent
- func (c *Component) AddGlobalData(key string, value any) RenderableComponent
- func (c *Component) AddTemplateFunction(name string, function interface{}) RenderableComponent
- func (c *Component) AddTemplateFunctions(funcs template.FuncMap) RenderableComponent
- func (c *Component) Attach(target string) RenderableComponent
- func (c *Component) Render(ctx context.Context) (template.HTML, error)
- func (c *Component) Reset() *Component
- func (c *Component) SetData(input map[string]any) RenderableComponent
- func (c *Component) SetGlobalData(input map[string]any) RenderableComponent
- func (c *Component) SetURL(url *url.URL)
- func (c *Component) With(r RenderableComponent, target string) RenderableComponent
- func (c *Component) Wrap(renderer RenderableComponent, target string) RenderableComponent
- type HTMX
- func (h *HTMX) HxHeader(r *http.Request) HxRequestHeader
- func (h *HTMX) HxResponseHeader(headers http.Header) *HxResponseHeader
- func (h *HTMX) NewHandler(w http.ResponseWriter, r *http.Request) *Handler
- func (h *HTMX) NewSSE(workerPoolSize int) error
- func (h *HTMX) SSEHandler(w http.ResponseWriter, r *http.Request, cl sse.Listener)
- func (h *HTMX) SSESend(message sse.Envelope)
- func (h *HTMX) SetLog(log Logger)
- type Handler
- func (h *Handler) Header() http.Header
- func (h *Handler) IsHxBoosted() bool
- func (h *Handler) IsHxHistoryRestoreRequest() bool
- func (h *Handler) IsHxRequest() bool
- func (h *Handler) JustWrite(data []byte)
- func (h *Handler) JustWriteHTML(html template.HTML)
- func (h *Handler) JustWriteJSON(data any)
- func (h *Handler) JustWriteString(s string)
- func (h *Handler) Location(li *LocationInput) error
- func (h *Handler) MustWrite(data []byte)
- func (h *Handler) MustWriteHTML(html template.HTML)
- func (h *Handler) MustWriteJSON(data any)
- func (h *Handler) MustWriteString(s string)
- func (h *Handler) PushURL(val string)
- func (h *Handler) ReSelect(val string)
- func (h *Handler) ReSwap(val string)
- func (h *Handler) ReSwapWithObject(s *Swap)
- func (h *Handler) ReTarget(val string)
- func (h *Handler) Redirect(val string)
- func (h *Handler) Refresh(val bool)
- func (h *Handler) Render(ctx context.Context, r RenderableComponent) (int, error)
- func (h *Handler) RenderPartial() bool
- func (h *Handler) ReplaceURL(val string)
- func (h *Handler) Request() HxRequestHeader
- func (h *Handler) ResponseHeader(header HxResponseKey) string
- func (h *Handler) StopPolling()
- func (h *Handler) Trigger(val string)
- func (h *Handler) TriggerAfterSettle(val string)
- func (h *Handler) TriggerAfterSettleWithObject(t *Trigger)
- func (h *Handler) TriggerAfterSwap(val string)
- func (h *Handler) TriggerAfterSwapWithObject(t *Trigger)
- func (h *Handler) TriggerCustom(custom, message string, vars ...map[string]any)
- func (h *Handler) TriggerError(message string, vars ...map[string]any)
- func (h *Handler) TriggerInfo(message string, vars ...map[string]any)
- func (h *Handler) TriggerSuccess(message string, vars ...map[string]any)
- func (h *Handler) TriggerWarning(message string, vars ...map[string]any)
- func (h *Handler) TriggerWithObject(t *Trigger)
- func (h *Handler) Write(data []byte) (n int, err error)
- func (h *Handler) WriteHTML(html template.HTML) (n int, err error)
- func (h *Handler) WriteHeader(code int)
- func (h *Handler) WriteJSON(data any) (n int, err error)
- func (h *Handler) WriteString(s string) (n int, err error)
- type HxRequestHeader
- type HxRequestHeaderKey
- type HxResponseHeader
- type HxResponseKey
- type LocationInput
- type Logger
- type RenderableComponent
- type Swap
- func (s *Swap) FocusScroll(focusScroll bool) *Swap
- func (s *Swap) IgnoreTitle(ignoreTitle bool) *Swap
- func (s *Swap) Scroll(direction SwapDirection, target ...string) *Swap
- func (s *Swap) ScrollBottom(target ...string) *Swap
- func (s *Swap) ScrollTop(target ...string) *Swap
- func (s *Swap) Settle(swap ...time.Duration) *Swap
- func (s *Swap) Show(direction SwapDirection, target ...string) *Swap
- func (s *Swap) ShowBottom(target ...string) *Swap
- func (s *Swap) ShowTop(target ...string) *Swap
- func (s *Swap) String() string
- func (s *Swap) Style(style SwapStyle) *Swap
- func (s *Swap) Swap(swap ...time.Duration) *Swap
- func (s *Swap) Transition(transition bool) *Swap
- type SwapDirection
- type SwapScrolling
- type SwapScrollingMode
- type SwapStyle
- type SwapTiming
- type SwapTimingMode
- type Trigger
- func (t *Trigger) AddError(message string, vars ...map[string]any)
- func (t *Trigger) AddEvent(event string) *Trigger
- func (t *Trigger) AddEventDetailed(event, message string) *Trigger
- func (t *Trigger) AddEventObject(event string, details map[string]any) *Trigger
- func (t *Trigger) AddInfo(message string, vars ...map[string]any)
- func (t *Trigger) AddSuccess(message string, vars ...map[string]any)
- func (t *Trigger) AddWarning(message string, vars ...map[string]any)
- func (t *Trigger) String() string
Constants ¶
const (
// StatusStopPolling is the status code that will stop htmx from polling
StatusStopPolling = 286
)
Variables ¶
var ( DefaultTemplateFuncs = template.FuncMap{} UseTemplateCache = true )
var ( DefaultSwapDuration = time.Duration(0 * time.Millisecond) DefaultSettleDelay = time.Duration(20 * time.Millisecond) DefaultNotificationKey = "showMessage" DefaultSSEWorkerPoolSize = 5 )
Functions ¶
func HxBoolToStr ¶
HxBoolToStr converts a boolean value to a string.
func HxStrToBool ¶
HxStrToBool converts a string to a boolean value.
func IsHxBoosted ¶ added in v1.5.1
IsHxBoosted returns true if the request is a htmx request and the request is boosted
func IsHxHistoryRestoreRequest ¶ added in v1.5.1
IsHxHistoryRestoreRequest returns true if the request is a htmx request and the request is a history restore request
func IsHxRequest ¶ added in v1.5.1
IsHxRequest returns true if the request is a htmx request.
func RenderPartial ¶ added in v1.5.1
RenderPartial returns true if the request is an HTMX request that is either boosted or a hx request, provided it is not a history restore request.
Types ¶
type Component ¶ added in v1.9.0
type Component struct {
// contains filtered or unexported fields
}
func NewComponent ¶ added in v1.9.0
func (*Component) AddData ¶ added in v1.11.0
func (c *Component) AddData(key string, value any) RenderableComponent
func (*Component) AddGlobalData ¶ added in v1.11.0
func (c *Component) AddGlobalData(key string, value any) RenderableComponent
func (*Component) AddTemplateFunction ¶ added in v1.11.0
func (c *Component) AddTemplateFunction(name string, function interface{}) RenderableComponent
func (*Component) AddTemplateFunctions ¶ added in v1.11.0
func (c *Component) AddTemplateFunctions(funcs template.FuncMap) RenderableComponent
func (*Component) Attach ¶ added in v1.11.0
func (c *Component) Attach(target string) RenderableComponent
Attach adds a template to the main component but doesn't pre-render it
func (*Component) Render ¶ added in v1.9.0
Render renders the given templates with the given data it has all the default template functions and the additional template functions that are added with AddTemplateFunction
func (*Component) SetData ¶ added in v1.9.0
func (c *Component) SetData(input map[string]any) RenderableComponent
SetData adds data to the component
func (*Component) SetGlobalData ¶ added in v1.11.0
func (c *Component) SetGlobalData(input map[string]any) RenderableComponent
func (*Component) With ¶ added in v1.9.0
func (c *Component) With(r RenderableComponent, target string) RenderableComponent
With adds a partial to the component
func (*Component) Wrap ¶ added in v1.9.0
func (c *Component) Wrap(renderer RenderableComponent, target string) RenderableComponent
Wrap wraps the component with the given renderer
type HTMX ¶
type HTMX struct {
// contains filtered or unexported fields
}
func (*HTMX) HxResponseHeader ¶ added in v1.4.0
func (h *HTMX) HxResponseHeader(headers http.Header) *HxResponseHeader
func (*HTMX) NewHandler ¶
NewHandler returns a new htmx handler.
func (*HTMX) NewSSE ¶ added in v1.7.0
NewSSE creates a new sse manager with the specified worker pool size.
func (*HTMX) SSEHandler ¶ added in v1.7.0
SSEHandler handles the server-sent events. this is a shortcut and is not the preferred way to handle sse.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
func (*Handler) Header ¶ added in v1.0.1
Header returns the header map that will be sent by WriteHeader
func (*Handler) IsHxBoosted ¶ added in v1.3.0
IsHxBoosted returns true if the request is a htmx request and the request is boosted
func (*Handler) IsHxHistoryRestoreRequest ¶ added in v1.3.0
IsHxHistoryRestoreRequest returns true if the request is a htmx request and the request is a history restore request
func (*Handler) IsHxRequest ¶ added in v1.3.0
IsHxRequest returns true if the request is a htmx request.
func (*Handler) JustWrite ¶ added in v1.6.0
JustWrite writes the data to the connection as part of an HTTP reply.
func (*Handler) JustWriteHTML ¶ added in v1.6.0
JustWriteHTML is a helper that writes HTML data to the connection.
func (*Handler) JustWriteJSON ¶ added in v1.6.0
JustWriteJSON is a helper that writes json data to the connection.
func (*Handler) JustWriteString ¶ added in v1.6.0
JustWriteString is a helper that writes string data to the connection.
func (*Handler) Location ¶
func (h *Handler) Location(li *LocationInput) error
Location can be used to trigger a client side redirection without reloading the whole page https://htmx.org/headers/hx-location/
func (*Handler) MustWrite ¶ added in v1.6.0
MustWrite writes the data to the connection as part of an HTTP reply.
func (*Handler) MustWriteHTML ¶ added in v1.6.0
MustWriteHTML is a helper that writes HTML data to the connection.
func (*Handler) MustWriteJSON ¶ added in v1.6.0
MustWriteJSON is a helper that writes json data to the connection.
func (*Handler) MustWriteString ¶ added in v1.6.0
MustWriteString is a helper that writes string data to the connection.
func (*Handler) PushURL ¶
PushURL pushes a new url into the history stack. https://htmx.org/headers/hx-push-url/
func (*Handler) ReSelect ¶ added in v1.1.0
ReSelect 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
func (*Handler) ReSwap ¶
ReSwap allows you to specify how the response will be swapped. See hx-swap for possible values https://htmx.org/attributes/hx-swap/
func (*Handler) ReSwapWithObject ¶ added in v1.3.0
ReSwapWithObject allows you to specify how the response will be swapped. See hx-swap for possible values https://htmx.org/attributes/hx-swap/
func (*Handler) ReTarget ¶
ReTarget a CSS selector that updates the target of the content update to a different element on the page
func (*Handler) Render ¶ added in v1.9.0
Render renders the given renderer with the given context and writes the output to the response writer
func (*Handler) RenderPartial ¶ added in v1.3.0
RenderPartial returns true if the request is an HTMX request that is either boosted or a standard request, provided it is not a history restore request.
func (*Handler) ReplaceURL ¶
ReplaceURL allows you to replace the current URL in the browser location history. https://htmx.org/headers/hx-replace-url/
func (*Handler) Request ¶ added in v1.0.1
func (h *Handler) Request() HxRequestHeader
Request returns the HxHeaders from the request
func (*Handler) ResponseHeader ¶ added in v1.4.0
func (h *Handler) ResponseHeader(header HxResponseKey) string
ResponseHeader returns the value of the response header
func (*Handler) StopPolling ¶ added in v1.1.0
func (h *Handler) StopPolling()
StopPolling sets the response status to 286, which will stop htmx from polling
func (*Handler) Trigger ¶
Trigger triggers events as soon as the response is received. https://htmx.org/headers/hx-trigger/
func (*Handler) TriggerAfterSettle ¶
TriggerAfterSettle trigger events after the settling step. https://htmx.org/headers/hx-trigger/
func (*Handler) TriggerAfterSettleWithObject ¶ added in v1.3.0
TriggerAfterSettleWithObject trigger events after the settling step. https://htmx.org/headers/hx-trigger/
func (*Handler) TriggerAfterSwap ¶
TriggerAfterSwap trigger events after the swap step. https://htmx.org/headers/hx-trigger/
func (*Handler) TriggerAfterSwapWithObject ¶ added in v1.3.0
TriggerAfterSwapWithObject trigger events after the swap step. https://htmx.org/headers/hx-trigger/
func (*Handler) TriggerCustom ¶ added in v1.5.0
func (*Handler) TriggerError ¶ added in v1.5.0
func (*Handler) TriggerInfo ¶ added in v1.5.0
func (*Handler) TriggerSuccess ¶ added in v1.5.0
func (*Handler) TriggerWarning ¶ added in v1.5.0
func (*Handler) TriggerWithObject ¶ added in v1.3.0
TriggerWithObject triggers events as soon as the response is received. https://htmx.org/headers/hx-trigger/
func (*Handler) WriteHTML ¶ added in v1.5.1
WriteHTML is a helper that writes HTML data to the connection.
func (*Handler) WriteHeader ¶ added in v1.0.1
WriteHeader sets the HTTP response header with the provided status code.
type HxRequestHeader ¶
type HxRequestHeader struct { HxBoosted bool HxCurrentURL string HxHistoryRestoreRequest bool HxPrompt string HxRequest bool HxTarget string HxTriggerName string HxTrigger string }
func HxRequestHeaderFromRequest ¶ added in v1.4.0
func HxRequestHeaderFromRequest(r *http.Request) HxRequestHeader
type HxRequestHeaderKey ¶ added in v1.5.1
type HxRequestHeaderKey string
const ( // ContextRequestHeader is the context key for the htmx request header. ContextRequestHeader = "htmx-request-header" HxRequestHeaderBoosted HxRequestHeaderKey = "HX-Boosted" HxRequestHeaderCurrentURL HxRequestHeaderKey = "HX-Current-URL" HxRequestHeaderHistoryRestoreRequest HxRequestHeaderKey = "HX-History-Restore-Request" HxRequestHeaderPrompt HxRequestHeaderKey = "HX-Prompt" HxRequestHeaderRequest HxRequestHeaderKey = "HX-Request" HxRequestHeaderTarget HxRequestHeaderKey = "HX-Target" HxRequestHeaderTriggerName HxRequestHeaderKey = "HX-Trigger-Name" HxRequestHeaderTrigger HxRequestHeaderKey = "HX-Trigger" )
func (HxRequestHeaderKey) String ¶ added in v1.5.1
func (x HxRequestHeaderKey) String() string
type HxResponseHeader ¶
type HxResponseHeader struct {
// contains filtered or unexported fields
}
func (*HxResponseHeader) Get ¶ added in v1.4.0
func (h *HxResponseHeader) Get(k HxResponseKey) string
func (*HxResponseHeader) Set ¶
func (h *HxResponseHeader) Set(k HxResponseKey, val string)
type HxResponseKey ¶
type HxResponseKey string
var ( HXLocation HxResponseKey = "HX-Location" // Allows you to do a client-side redirect that does not do a full page reload HXPushUrl HxResponseKey = "HX-Push-Url" // pushes a new url into the history stack HXRedirect HxResponseKey = "HX-Redirect" // can be used to do a client-side redirect to a new location HXRefresh HxResponseKey = "HX-Refresh" // if set to "true" the client side will do a full refresh of the page HXReplaceUrl HxResponseKey = "HX-Replace-Url" // replaces the current URL in the location bar HXReswap HxResponseKey = "HX-Reswap" // Allows you to specify how the response will be swapped. See hx-swap for possible values HXRetarget HxResponseKey = "HX-Retarget" // A CSS selector that updates the target of the content update to a different element on the page HXReselect HxResponseKey = "HX-Reselect" // 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 HXTrigger HxResponseKey = "HX-Trigger" // allows you to trigger client side events, see the documentation for more info HXTriggerAfterSettle HxResponseKey = "HX-Trigger-After-Settle" // allows you to trigger client side events, see the documentation for more info HXTriggerAfterSwap HxResponseKey = "HX-Trigger-After-Swap" // allows you to trigger client side events, see the documentation for more info )
func (HxResponseKey) String ¶
func (h HxResponseKey) String() string
type LocationInput ¶
type LocationInput struct { Source string `json:"source"` // source - the source element of the request Event string `json:"event"` //event - an event that "triggered" the request Handler string `json:"handler"` //handler - a callback that will handle the response HTML Target string `json:"target"` //target - the target to swap the response into Swap string `json:"swap"` //swap - how the response will be swapped in relative to the target Values map[string]interface{} `json:"values"` //values - values to submit with the request Header map[string]interface{} `json:"headers"` //headers - headers to submit with the request }
type RenderableComponent ¶ added in v1.9.0
type RenderableComponent interface { Render(ctx context.Context) (template.HTML, error) Wrap(renderer RenderableComponent, target string) RenderableComponent With(r RenderableComponent, target string) RenderableComponent Attach(target string) RenderableComponent SetData(input map[string]any) RenderableComponent AddData(key string, value any) RenderableComponent SetGlobalData(input map[string]any) RenderableComponent AddGlobalData(key string, value any) RenderableComponent AddTemplateFunction(name string, function interface{}) RenderableComponent AddTemplateFunctions(funcs template.FuncMap) RenderableComponent SetURL(url *url.URL) Reset() *Component // contains filtered or unexported methods }
type Swap ¶ added in v1.3.0
type Swap struct {
// contains filtered or unexported fields
}
func (*Swap) FocusScroll ¶ added in v1.3.0
FocusScroll enables or disables the focus scroll behaviour
func (*Swap) IgnoreTitle ¶ added in v1.3.0
IgnoreTitle enables or disables the Title
func (*Swap) Scroll ¶ added in v1.3.0
func (s *Swap) Scroll(direction SwapDirection, target ...string) *Swap
Scroll sets the scrolling behavior to scroll to the top or bottom
func (*Swap) ScrollBottom ¶ added in v1.3.0
ScrollBottom sets the scrolling behavior to scroll to the bottom of the target element
func (*Swap) ScrollTop ¶ added in v1.3.0
ScrollTop sets the scrolling behavior to scroll to the top of the target element
func (*Swap) Settle ¶ added in v1.3.0
Settle modifies the amount of time that htmx will wait after receiving a response to settle the content
func (*Swap) Show ¶ added in v1.3.0
func (s *Swap) Show(direction SwapDirection, target ...string) *Swap
Show sets the scrolling behavior to scroll to the top or bottom
func (*Swap) ShowBottom ¶ added in v1.3.0
ShowBottom sets the scrolling behavior to scroll to the bottom of the target element
func (*Swap) ShowTop ¶ added in v1.3.0
ShowTop sets the scrolling behavior to scroll to the top of the target element
func (*Swap) Style ¶ added in v1.3.0
Style sets the style of the swap, default is innerHTML and can be changed in htmx.config.defaultSwapStyle
func (*Swap) Swap ¶ added in v1.3.0
Swap modifies the amount of time that htmx will wait after receiving a response to swap the content
func (*Swap) Transition ¶ added in v1.3.0
Transition enables or disables the transition see : https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API
type SwapDirection ¶ added in v1.3.0
type SwapDirection string
SwapDirection modifies the scrolling behavior of the target element
const ( SwapDirectionTop SwapDirection = "top" SwapDirectionBottom SwapDirection = "bottom" )
func (SwapDirection) String ¶ added in v1.3.0
func (s SwapDirection) String() string
type SwapScrolling ¶ added in v1.3.0
type SwapScrolling struct {
// contains filtered or unexported fields
}
func (*SwapScrolling) String ¶ added in v1.3.0
func (s *SwapScrolling) String() string
type SwapScrollingMode ¶ added in v1.3.0
type SwapScrollingMode string
const ( // ScrollingScroll You can also change the scrolling behavior of the target element by using the scroll and show modifiers, both of which take the values top and bottom ScrollingScroll SwapScrollingMode = "scroll" // ScrollingShow You can also change the scrolling behavior of the target element by using the scroll and show modifiers, both of which take the values top and bottom ScrollingShow SwapScrollingMode = "show" )
func (SwapScrollingMode) String ¶ added in v1.3.0
func (s SwapScrollingMode) String() string
type SwapStyle ¶ added in v1.3.0
type SwapStyle string
const ( // SwapInnerHTML replaces the inner html of the target element SwapInnerHTML SwapStyle = "innerHTML" // SwapOuterHTML replaces the entire target element with the response SwapOuterHTML SwapStyle = "outerHTML" // SwapBeforeBegin insert the response before the target element SwapBeforeBegin SwapStyle = "beforebegin" // SwapAfterBegin insert the response before the first child of the target element SwapAfterBegin SwapStyle = "afterbegin" // SwapBeforeEnd insert the response after the last child of the target element SwapBeforeEnd SwapStyle = "beforeend" // SwapAfterEnd insert the response after the target element SwapAfterEnd SwapStyle = "afterend" // SwapDelete deletes the target element regardless of the response SwapDelete SwapStyle = "delete" // SwapNone does not append content from response (out of band items will still be processed). SwapNone SwapStyle = "none" )
type SwapTiming ¶ added in v1.3.0
type SwapTiming struct {
// contains filtered or unexported fields
}
func (*SwapTiming) String ¶ added in v1.3.0
func (s *SwapTiming) String() string
type SwapTimingMode ¶ added in v1.3.0
type SwapTimingMode string
SwapTimingMode modifies the amount of time that htmx will wait after receiving a response to swap or settle the content
const ( // TimingSwap You can modify the amount of time that htmx will wait after receiving a response to swap the content by including a swap modifier TimingSwap SwapTimingMode = "swap" // TimingSettle you can modify the time between the swap and the settle logic by including a settle modifier: TimingSettle SwapTimingMode = "settle" )
func (SwapTimingMode) String ¶ added in v1.3.0
func (s SwapTimingMode) String() string
type Trigger ¶ added in v1.3.0
type Trigger struct {
// contains filtered or unexported fields
}
func (*Trigger) AddEventDetailed ¶ added in v1.3.0
AddEventDetailed adds a trigger to the Trigger set
func (*Trigger) AddEventObject ¶ added in v1.3.0
AddEventObject adds a trigger to the Trigger set