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.
Request & Dispatch Architecture
Section titled “Request & Dispatch Architecture”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 viaslot('header', ...)orplace(...). - Value Gates: Declare
$valueGateKeysor callhasValue()/renderIfHasValue()so optional wrappers disappear cleanly when a prop is empty. - Shape Compilation: The first time a
Uirenders a given combination of active zones and$shapeParams,HtmxCachecompiles its structural template once and caches it on disk (site/assets/cache/Htmx/, bounded bycacheMaxShapesPerClass, default64), making subsequent renders near-instant. - Safe Attributes:
AttributeBagescapes attribute values and blocks dangerous inline event handlers (on*and(data-)hx-on*).
2. Stateful Component & Actions
Section titled “2. Stateful Component & Actions”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 signedhx__statehidden input or header) -> on subsequent HTMX requests:hydrate()->authorize()->authorizeAction()->executeAction()->render(). - Mutating vs. Query Actions: State-changing methods accept
POST,PUT,PATCH, orDELETEand automatically require$session->CSRF->validate(). Read-only methods prefixed withquery(such asqueryFilter()) can be invoked overGETorQUERYwithout mutating state. - Confined Views: Views rendered via
setView()execute insideConfinedViewRenderer, which confines template paths to the configured directory and prevents path traversal.
3. HKDF-Signed hx__state Payloads
Section titled “3. HKDF-Signed hx__state Payloads”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.
4. Request, Response & Fragment Services
Section titled “4. Request, Response & Fragment Services”Every request exposes three typed sub-services on $this->wire()->htmx:
$htmx->request(Request): DetectsisHtmx(),isBoosted(),isHistoryRestore(),isFullPage(),isPartial(),isQuery(), andisQueryActionRequest(), plusvalidateCsrf()andfake()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()).