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
- Variables
- func Boosted(c *router.Ctx) bool
- func CurrentURL(c *router.Ctx) string
- func Fragment(components ...view.Component) router.Response
- func FragmentStatus(code int, components ...view.Component) router.Response
- func HistoryRestore(c *router.Ctx) bool
- func Location(c *router.Ctx, url string) router.Response
- func Open(fn func(s *Stream) error) router.Response
- func Partial(c *router.Ctx) bool
- func Prompt(c *router.Ctx) string
- func PushURL(c *router.Ctx, url string)
- func Redirect(c *router.Ctx, url string) router.Response
- func Refresh(c *router.Ctx)
- func ReplaceURL(c *router.Ctx, url string)
- func Reselect(c *router.Ctx, selector string)
- func Reswap(c *router.Ctx, spec string)
- func Retarget(c *router.Ctx, selector string)
- func Target(c *router.Ctx) string
- func Trigger(c *router.Ctx, event any) error
- func TriggerAfterSettle(c *router.Ctx, event any) error
- func TriggerAfterSwap(c *router.Ctx, event any) error
- func TriggerID(c *router.Ctx) string
- func TriggerName(c *router.Ctx) string
- type Fault
- type Stream
Constants ¶
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.
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.
const ContentType = "text/html; charset=utf-8"
ContentType is the content type of a fragment.
Variables ¶
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 CurrentURL ¶
CurrentURL returns the address of the browser.
func Fragment ¶
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 ¶
FragmentStatus renders components with this status, for example 422.
func HistoryRestore ¶
HistoryRestore reports whether htmx restores a page from its history.
func Location ¶
Location asks htmx for a client side navigation, which loads the page into the current document.
func Open ¶
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 ¶
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 Redirect ¶
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 ReplaceURL ¶
ReplaceURL replaces the address of the history.
func Trigger ¶
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 ¶
TriggerAfterSettle raises an event after the settle.
func TriggerAfterSwap ¶
TriggerAfterSwap raises an event after the swap.
func TriggerName ¶
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.
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) Done ¶
func (s *Stream) Done() <-chan struct{}
Done returns a channel that closes when the client disconnects.