htmx

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package htmx holds the htmx adapter, S12.

It provides the fragment response, the request helpers and the response headers that htmx reads. It renders a component, so a page and a fragment share one template.

The package is an optional import. The view package does not import it, and the scaffolded ui package does not import it. A component reads Partial and renders a fragment, so one template serves both shapes. See the SDD, S12.

The headers follow htmx 2.

Index

Constants

View Source
const (
	// RequestHeader marks a request that htmx made.
	RequestHeader = "HX-Request"
	// BoostedHeader marks a request from a boosted link or form.
	BoostedHeader = "HX-Boosted"
	// TargetHeader carries the identifier of the target element.
	TargetHeader = "HX-Target"
	// TriggerHeader carries the identifier of the element that triggered.
	TriggerHeader = "HX-Trigger"
	// TriggerNameHeader carries the name of that element.
	TriggerNameHeader = "HX-Trigger-Name"
	// CurrentURLHeader carries the address of the browser.
	CurrentURLHeader = "HX-Current-URL"
	// PromptHeader carries the answer to hx-prompt.
	PromptHeader = "HX-Prompt"
	// HistoryRestoreHeader marks a history restoration request.
	HistoryRestoreHeader = "HX-History-Restore-Request"
)

The headers that htmx sends on a request.

View Source
const (
	// LocationHeader asks htmx for a client side navigation.
	LocationHeader = "HX-Location"
	// PushURLHeader pushes an address into the history.
	PushURLHeader = "HX-Push-Url"
	// ReplaceURLHeader replaces the address of the history.
	ReplaceURLHeader = "HX-Replace-Url"
	// RedirectHeader asks the browser for a whole page navigation.
	RedirectHeader = "HX-Redirect"
	// RefreshHeader asks the browser for a whole page reload.
	RefreshHeader = "HX-Refresh"
	// ReswapHeader states how htmx swaps the answer.
	ReswapHeader = "HX-Reswap"
	// RetargetHeader states the element that htmx swaps.
	RetargetHeader = "HX-Retarget"
	// ReselectHeader states the part of the answer that htmx takes.
	ReselectHeader = "HX-Reselect"
	// TriggerAfterSwapHeader triggers an event after the swap.
	TriggerAfterSwapHeader = "HX-Trigger-After-Swap"
	// TriggerAfterSettleHeader triggers an event after the settle.
	TriggerAfterSettleHeader = "HX-Trigger-After-Settle"
)

The headers that htmx reads on a response.

View Source
const ContentType = "text/html; charset=utf-8"

ContentType is the content type of a fragment.

Variables

View Source
var ErrClosed = sse.ErrClosed

ErrClosed states that the client closed the stream. Send returns it after a disconnect, so the handler stops and the goroutine ends.

Functions

func Boosted

func Boosted(c *router.Ctx) bool

Boosted reports whether a boosted link or form made this request.

func CurrentURL

func CurrentURL(c *router.Ctx) string

CurrentURL returns the address of the browser.

func Fragment

func Fragment(components ...view.Component) router.Response

Fragment renders one component or several with 200.

An out of band swap is an attribute of the template, so a second component carries hx-swap-oob and reaches another place on the page.

return htmx.Fragment(views.Row(row), views.Count(n)), nil

func FragmentStatus

func FragmentStatus(code int, components ...view.Component) router.Response

FragmentStatus renders components with this status, for example 422.

func HistoryRestore

func HistoryRestore(c *router.Ctx) bool

HistoryRestore reports whether htmx restores a page from its history.

func Location

func Location(c *router.Ctx, url string) router.Response

Location asks htmx for a client side navigation, which loads the page into the current document.

func Open

func Open(fn func(s *Stream) error) router.Response

Open returns a Response that opens a server sent event stream and runs fn.

The handler holds the request until fn returns, so fn must select on Done. A disconnect makes every send return ErrClosed, so a loop that ignores Done still ends.

func Partial

func Partial(c *router.Ctx) bool

Partial reports whether htmx made this request. A component renders one fragment for a partial request and the whole page for any other.

if htmx.Partial(c) {
    return htmx.Fragment(views.Row(row)), nil
}
return view.View(pages.Rows(rows)), nil

func Prompt

func Prompt(c *router.Ctx) string

Prompt returns the answer that hx-prompt collected.

func PushURL

func PushURL(c *router.Ctx, url string)

PushURL pushes an address into the history of the browser.

func Redirect

func Redirect(c *router.Ctx, url string) router.Response

Redirect asks the browser for a whole page navigation. htmx reads the header of a 200 answer, so the response carries no status of its own.

func Refresh

func Refresh(c *router.Ctx)

Refresh asks the browser to load the whole page again.

func ReplaceURL

func ReplaceURL(c *router.Ctx, url string)

ReplaceURL replaces the address of the history.

func Reselect

func Reselect(c *router.Ctx, selector string)

Reselect states the part of the answer that htmx takes.

func Reswap

func Reswap(c *router.Ctx, spec string)

Reswap states how htmx swaps the answer, such as beforeend.

func Retarget

func Retarget(c *router.Ctx, selector string)

Retarget states the element that htmx swaps.

func Target

func Target(c *router.Ctx) string

Target returns the identifier of the target element.

func Trigger

func Trigger(c *router.Ctx, event any) error

Trigger asks htmx to raise an event after the answer arrives.

A string names one event. A map names one event for each key and carries a detail, which encodes as JSON.

func TriggerAfterSettle

func TriggerAfterSettle(c *router.Ctx, event any) error

TriggerAfterSettle raises an event after the settle.

func TriggerAfterSwap

func TriggerAfterSwap(c *router.Ctx, event any) error

TriggerAfterSwap raises an event after the swap.

func TriggerID

func TriggerID(c *router.Ctx) string

TriggerID returns the identifier of the element that triggered the request.

func TriggerName

func TriggerName(c *router.Ctx) string

TriggerName returns the name of the element that triggered the request.

Types

type Fault

type Fault struct {
	// Message states the fault.
	Message string `json:"message"`
	// Repair states what to do. It is one sentence.
	Repair string `json:"repair"`
}

Fault is one htmx fault. It states what went wrong and states the repair in one sentence. See DX-7.

func (*Fault) Error

func (f *Fault) Error() string

Error returns the message and the repair.

type Stream

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

Stream writes fragments to one client. The htmx SSE extension reads them: an element with sse-swap names the event, and the data lines hold the markup.

func (*Stream) Ctx

func (s *Stream) Ctx() *router.Ctx

Ctx returns the request that opened the stream.

func (*Stream) Done

func (s *Stream) Done() <-chan struct{}

Done returns a channel that closes when the client disconnects.

func (*Stream) Send

func (s *Stream) Send(event string, c view.Component) error

Send renders a component and writes it as one event.

<div hx-ext="sse" sse-connect="/rows" sse-swap="row"></div>

func (*Stream) SendHTML

func (s *Stream) SendHTML(event, html string) error

SendHTML writes markup as one event. A line of the markup becomes one data line, because a frame cannot carry a new line.

Jump to

Keyboard shortcuts

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