Skip to content

5-Minute Quickstart

This quickstart builds the Aegean Voyages (aegeanvoyages.example) live charter fleet filter from site/modules/Htmx/examples/real-world/ using a stateless YachtCardUi and a stateful CharterFilterComponent.

  1. Scaffold a stateless Ui and stateful Component (or copy from examples/real-world/):

    Terminal window
    php vendor/bin/wire make:htmx:ui YachtCardUi
    php vendor/bin/wire make:htmx:component CharterFilterComponent
  2. Define the stateless YachtCardUi (site/templates/ui/YachtCardUi.php):

    <?php declare(strict_types=1);
    namespace ProcessWire;
    use Totoglu\Htmx\Ui;
    final class YachtCardUi extends Ui
    {
    protected array $zones = ['media', 'body', 'footer'];
    protected array $defaultZones = ['body'];
    protected array $valueGateKeys = ['badge'];
    protected function renderTemplate(): string
    {
    $title = $this->wire()->sanitizer->entities((string) $this->param('title', ''));
    $port = $this->wire()->sanitizer->entities((string) $this->param('port', 'Gocek'));
    $rate = $this->wire()->sanitizer->entities((string) $this->param('weeklyRate', ''));
    $badge = $this->renderIfHasValue('badge', fn (string $v): string =>
    '<span class="badge">' . $this->wire()->sanitizer->entities($v) . '</span>'
    );
    return <<<HTML
    <article {$this->attrFor('root', ['class' => 'yacht-card'])}>
    {$this->slot('media')}
    <div class="yacht-card__body">
    {$badge}
    <h3>{$title}</h3>
    <p>{$port} | {$rate}</p>
    {$this->slot('body')}
    </div>
    {$this->slot('footer')}
    </article>
    HTML;
    }
    }
  3. Define the stateful CharterFilterComponent (site/templates/components/CharterFilterComponent.php):

    <?php declare(strict_types=1);
    namespace ProcessWire;
    use Totoglu\Htmx\Component;
    final class CharterFilterComponent extends Component
    {
    public string $port = '';
    public int $minCabins = 0;
    public function queryFilter(): void
    {
    $input = $this->wire()->input;
    $this->port = $this->wire()->sanitizer->pageName((string) $input->get('port'));
    $this->minCabins = max(0, (int) $input->get('minCabins'));
    }
    public function render(): string
    {
    $selector = 'template=yacht, limit=12, sort=title';
    if ($this->port !== '') {
    $selector .= ', home_port=' . $this->wire()->sanitizer->selectorValue($this->port);
    }
    if ($this->minCabins > 0) {
    $selector .= ', cabins>=' . $this->minCabins;
    }
    $cards = '';
    foreach ($this->wire()->pages->find($selector) as $yacht) {
    $cards .= YachtCardUi::make($this->wire(), [
    'title' => (string) $yacht->title,
    'port' => (string) $yacht->get('home_port'),
    'weeklyRate' => (string) $yacht->get('weekly_rate_eur') . ' EUR/wk',
    'badge' => $yacht->get('featured') ? 'Featured' : '',
    ]);
    }
    $url = $this->requestUrl('queryFilter');
    $state = $this->renderStatePayload();
    return <<<HTML
    <section id="fleet-filter" hx-target="this" hx-swap="outerHTML">
    <form hx-get="{$url}" hx-trigger="change">
    {$state}
    <select name="port">
    <option value="">All Ports</option>
    <option value="gocek">Gocek</option>
    <option value="bodrum">Bodrum</option>
    </select>
    <input type="number" name="minCabins" min="0" value="{$this->minCabins}">
    </form>
    <div class="fleet-grid">{$cards}</div>
    </section>
    HTML;
    }
    }
  4. Register the component in site/ready.php and render it in your template:

    site/ready.php
    $wire->htmx->registerComponent('charter-filter', \ProcessWire\CharterFilterComponent::class);
    // site/templates/fleet.php
    echo $htmx->renderComponent('charter-filter', ['port' => 'gocek']);

Next steps