Help us shape the future of Go! Take the 2026 Go Developer Survey

htmx

package
v0.28.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package htmx provides helpers for htmx 4: reading htmx request headers, building response headers, and rendering <hx-partial> and out-of-band fragments. See https://four.htmx.org/reference/headers/.

See https://github.com/AlexGalhardo/vukem/blob/main/docs/frontend.md.

Example
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/AlexGalhardo/vukem/htmx"
)

func main() {
	h := func(w http.ResponseWriter, r *http.Request) {
		if !htmx.Is(r) {
			_, _ = fmt.Fprint(w, "<html>full page</html>")
			return
		}
		htmx.NewResponse(w).Trigger("post-created", nil).PushURL("/posts/1")
		_, _ = fmt.Fprint(w, "<li>Hello</li>")
	}

	req := httptest.NewRequest(http.MethodPost, "/posts", http.NoBody)
	req.Header.Set(htmx.HeaderRequest, "true")
	rec := httptest.NewRecorder()
	h(rec, req)

	fmt.Println(rec.Header().Get("HX-Trigger"))
	fmt.Println(rec.Header().Get("HX-Push-Url"))
	fmt.Println(rec.Body.String())
}
Output:
{"post-created":null}
/posts/1
<li>Hello</li>

Index

Examples

Constants

View Source
const (
	HeaderRequest        = "HX-Request"
	HeaderRequestType    = "HX-Request-Type"
	HeaderCurrentURL     = "HX-Current-URL"
	HeaderSource         = "HX-Source"
	HeaderTarget         = "HX-Target"
	HeaderBoosted        = "HX-Boosted"
	HeaderHistoryRestore = "HX-History-Restore-Request"
)

Request headers sent by htmx 4.

View Source
const (
	HeaderTrigger    = "HX-Trigger"
	HeaderLocation   = "HX-Location"
	HeaderRedirect   = "HX-Redirect"
	HeaderRefresh    = "HX-Refresh"
	HeaderPushURL    = "HX-Push-Url"
	HeaderReplaceURL = "HX-Replace-Url"
	HeaderRetarget   = "HX-Retarget"
	HeaderReswap     = "HX-Reswap"
	HeaderReselect   = "HX-Reselect"
)

Response headers understood by htmx 4.

Variables

This section is empty.

Functions

func CurrentURL

func CurrentURL(r *http.Request) string

CurrentURL returns the browser URL when the request started.

func Is

func Is(r *http.Request) bool

Is reports whether r was sent by htmx.

func IsBoosted

func IsBoosted(r *http.Request) bool

IsBoosted reports whether the request comes from hx-boost.

func IsHistoryRestore

func IsHistoryRestore(r *http.Request) bool

IsHistoryRestore reports a back/forward navigation re-fetch; it must be answered with a full page (htmx 4 has no local history cache).

func IsPartial

func IsPartial(r *http.Request) bool

IsPartial reports whether htmx asked for a fragment (targeted swap). Full requests (body swaps, boosted navigation, history restores) need the whole page.

func OOB

func OOB(id, swap string, content template.HTML) template.HTML

OOB renders content as an out-of-band swap into the element with id.

Example
package main

import (
	"fmt"

	"github.com/AlexGalhardo/vukem/htmx"
)

func main() {
	fmt.Println(htmx.OOB("count", "innerHTML", "3 posts"))
}
Output:
<div id="count" hx-swap-oob="innerHTML">3 posts</div>

func Partial

func Partial(target, swap string, content template.HTML) template.HTML

Partial wraps content in an <hx-partial> element so one response can update several targets. Attribute values are escaped.

Types

type Element

type Element struct {
	Tag string
	ID  string
}

Element is a "tag#id" reference from HX-Source or HX-Target.

func Source

func Source(r *http.Request) Element

Source returns the element that triggered the request.

func Target

func Target(r *http.Request) Element

Target returns the target element.

type LocationOptions

type LocationOptions struct {
	Path   string            `json:"path"`
	Target string            `json:"target,omitempty"`
	Swap   string            `json:"swap,omitempty"`
	Select string            `json:"select,omitempty"`
	Values map[string]any    `json:"values,omitempty"`
	Header map[string]string `json:"headers,omitempty"`
}

LocationOptions configures an HX-Location navigation.

type Response

type Response struct {
	// contains filtered or unexported fields
}

Response sets htmx response headers on w. Methods chain:

htmx.NewResponse(w).Trigger("saved", nil).PushURL("/posts/1")

func NewResponse

func NewResponse(w http.ResponseWriter) *Response

NewResponse returns a Response writing headers to w. It also adds "Vary: HX-Request" so caches keep full pages and fragments apart.

func (*Response) Location

func (r *Response) Location(path string) *Response

Location navigates without a full reload (like a boosted link).

func (*Response) LocationWith

func (r *Response) LocationWith(o LocationOptions) *Response

LocationWith navigates with options.

func (*Response) PushURL

func (r *Response) PushURL(url string) *Response

PushURL pushes url into the browser history.

func (*Response) Redirect

func (r *Response) Redirect(url string) *Response

Redirect performs a full-page redirect.

func (*Response) Refresh

func (r *Response) Refresh() *Response

Refresh reloads the page.

func (*Response) ReplaceURL

func (r *Response) ReplaceURL(url string) *Response

ReplaceURL replaces the current history entry.

func (*Response) Reselect

func (r *Response) Reselect(selector string) *Response

Reselect picks part of the response (CSS selector).

func (*Response) Reswap

func (r *Response) Reswap(style string) *Response

Reswap changes the swap style ("innerHTML", "outerMorph", ...).

func (*Response) Retarget

func (r *Response) Retarget(selector string) *Response

Retarget changes the swap target (CSS selector).

func (*Response) Trigger

func (r *Response) Trigger(event string, detail any) *Response

Trigger fires a client-side event; detail (may be nil) is sent as JSON. Multiple triggers are merged into one header.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL