Skip to content

Core Concepts

Htmx separates server-driven hypermedia into two complementary building blocks: Stateless Ui elements for pure presentation and Stateful Component classes for interactive server actions.

flowchart TD
    A["Browser triggers HTMX request to /hx/req"] --> B["ProcessWire::Htmx::handleEndpoint()"]
    B --> C["Resolve registered Component class"]
    C --> D["SignedStateCodec verifies HKDF-SHA256 hx__state"]
    D --> E{"Mutating verb (POST/PUT/PATCH/DELETE)?"}
    E -- Yes --> F["Validate $session->CSRF + authorizeAction()"]
    E -- No --> G["Verify query* read-only action + authorizeAction()"]
    F --> H["hydrate() -> executeAction() -> render()"]
    G --> H
    H --> I["Emit HTML + optional <hx-partial> & HX-* headers"]

1. Stateless Ui (Zones, Slots & Shape Compilation)

Section titled “1. Stateless Ui (Zones, Slots & Shape Compilation)”

A Ui class (extending Totoglu\Htmx\Ui) is a pure, stateless HTML primitive such as a yacht card, badge, or modal shell:

  • Zones & Slots: Organize markup into named regions ($zones, $defaultZones) and fill them via slot('header', ...) or place(...).
  • Value Gates: Declare $valueGateKeys or call hasValue() / renderIfHasValue() so optional wrappers disappear cleanly when a prop is empty.
  • Shape Compilation: The first time a Ui renders a given combination of active zones and $shapeParams, HtmxCache compiles its structural template once and caches it on disk (site/assets/cache/Htmx/, bounded by cacheMaxShapesPerClass, default 64), making subsequent renders near-instant.
  • Safe Attributes: AttributeBag escapes attribute values and blocks dangerous inline event handlers (on* and (data-)hx-on*).

A Component class (extending Totoglu\Htmx\Component) renders interactive HTML fragments that talk back to ProcessWire over the unified endpointUrl (default /hx/req):

  • Lifecycle: fill() -> mount() (initial render) -> renderStatePayload() (emits signed hx__state hidden input or header) -> on subsequent HTMX requests: hydrate() -> authorize() -> authorizeAction() -> executeAction() -> render().
  • Mutating vs. Query Actions: State-changing methods accept POST, PUT, PATCH, or DELETE and automatically require $session->CSRF->validate(). Read-only methods prefixed with query (such as queryFilter()) can be invoked over GET or QUERY without mutating state.
  • Confined Views: Views rendered via setView() execute inside ConfinedViewRenderer, which confines template paths to the configured directory and prevents path traversal.

Stateful components persist their public state across HTMX swaps inside a signed hx__state payload produced by SignedStateCodec:

  • Derives its MAC key with HKDF-SHA256 (totoglu-htmx:component-state:v1) from ProcessWire’s $config->userAuthSalt (must be at least 16 characters).
  • Binds every token to five context claims: __uid (current user ID), __sid (session fingerprint), __ep (normalized endpoint path), __pid (originating page ID), and __adm (admin vs. frontend context).
  • Any modified byte or cross-user replay is rejected before hydrate() runs.

Every request exposes three typed sub-services on $this->wire()->htmx:

  • $htmx->request (Request): Detects isHtmx(), isBoosted(), isHistoryRestore(), isFullPage(), isPartial(), isQuery(), and isQueryActionRequest(), plus validateCsrf() and fake() for unit tests.
  • $htmx->response (Response): Emits HTMX 4 response headers (redirect(), refresh(), location(), pushUrl(), replaceUrl(), retarget(), reselect(), reswap(), noSwap(), trigger(), triggerAfterSwap(), triggerAfterSettle(), stopPolling(), vary()).
  • $htmx->fragment (Fragment): Renders multi-target <hx-partial> elements (addPartial(), renderPartials()) and isolated PHP fragment files (renderFile()).

Next steps