Stateless Ui, Zones & Cache
Stateless Ui classes (Totoglu\Htmx\Ui) give ProcessWire templates a clean, composable HTML primitive system backed by automatic shape compilation in HtmxCache.
Anatomy of a Ui Class
Section titled “Anatomy of a Ui Class”| Property / Method | Purpose |
|---|---|
Ui::make($wire, $params) |
Instantiates a Ui primitive wired to ProcessWire with a typed ParameterBag. |
$shapeParams |
Parameter names that affect structural markup (for example ['variant', 'size']), included in the compiled shape key. |
$slotParams |
Parameter names treated as named slot content. |
$zones / $defaultZones |
Allowed named layout regions and which zones render by default. |
$valueGateKeys |
Parameter keys checked by hasValue() / renderIfHasValue() to omit empty wrappers cleanly. |
slot(string $name, string $default = '') |
Reads a filled slot or returns its fallback HTML. |
place(string $zone, string|callable $content) |
Places content into a named zone validated against getValidZones(). |
attrFor(string $part, array $defaults = []) / for(...) |
Merges safe HTML attributes via AttributeBag, blocking on* and (data-)hx-on* event attributes. |
raw(string $html) |
Explicitly marks pre-sanitized HTML to bypass double-escaping. |
cache() / memo(string $key, callable $fn) |
Caches expensive render fragments or per-request memoized values via HtmxCache. |
Zones, Slots & Value Gates Example
Section titled “Zones, Slots & Value Gates Example”<?php declare(strict_types=1);
namespace ProcessWire;
use Totoglu\Htmx\Ui;
final class CharterBadgeUi extends Ui{ protected array $shapeParams = ['variant']; protected array $zones = ['icon', 'label']; protected array $defaultZones = ['label']; protected array $valueGateKeys = ['tooltip'];
protected function renderTemplate(): string { $variant = $this->wire()->sanitizer->pageName((string) $this->param('variant', 'info')); $label = $this->wire()->sanitizer->entities((string) $this->param('label', '')); $tooltip = $this->renderIfHasValue('tooltip', fn (string $tip): string => ' title="' . $this->wire()->sanitizer->entities($tip) . '"' );
return sprintf( '<span %s%s>%s%s</span>', $this->attrFor('root', ['class' => "badge badge--{$variant}"]), $tooltip, $this->slot('icon'), $label ); }}How HtmxCache Shape Compilation Works
Section titled “How HtmxCache Shape Compilation Works”$this->wire()->htmxCache (Totoglu\Htmx\HtmxCache) accelerates both Ui and Component rendering:
getOrCompileUi(): Compiles each uniqueresolveShape()signature viacompileTemplate()and writes the result tosite/assets/cache/Htmx/, capped atcacheMaxShapesPerClass(default64) per class.getOrCompileComponentSchema(): Caches reflected public property schemas for statefulComponentclasses so hydration does not repeat reflection on every request.getComponentRender()/putComponentRender(): Stores optional per-component render cache entries when a component callsenableRenderCache().getMemo()/putMemo(): Provides per-request and cross-request memoization helpers.stats(),prune(),clear(),resetMemory(): Inspect cache counts, prune stale files by age, or flush everything via PHP orphp vendor/bin/wire htmx:cache:clear.