Skip to content

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.

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.
<?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
);
}
}

$this->wire()->htmxCache (Totoglu\Htmx\HtmxCache) accelerates both Ui and Component rendering:

  • getOrCompileUi(): Compiles each unique resolveShape() signature via compileTemplate() and writes the result to site/assets/cache/Htmx/, capped at cacheMaxShapesPerClass (default 64) per class.
  • getOrCompileComponentSchema(): Caches reflected public property schemas for stateful Component classes so hydration does not repeat reflection on every request.
  • getComponentRender() / putComponentRender(): Stores optional per-component render cache entries when a component calls enableRenderCache().
  • 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 or php vendor/bin/wire htmx:cache:clear.

Next steps