Skip to content

Quickstart

This walkthrough shows how to render an article template through Inertia in Hybrid Mode while keeping classic ProcessWire templates untouched.

  1. Enable the template for Inertia

    Open Modules > Configure > Inertia, select Selected Templates Only (Hybrid Mode), and add article to Inertia Templates.

    You can also register templates programmatically in site/init.php or site/ready.php:

    <?php
    declare(strict_types=1);
    namespace ProcessWire;
    inertia()->forTemplates(['article']);
  2. Add InteractWithInertia to site/classes/DefaultPage.php

    DefaultPage defines shared metadata (inertiaViewData()) and base props (inertiaWithData()) inherited by every template Page Class:

    <?php
    declare(strict_types=1);
    namespace ProcessWire;
    use Totoglu\Inertia\InteractWithInertia;
    class DefaultPage extends Page
    {
    use InteractWithInertia;
    public function browserTitle(): string
    {
    return (string) $this->title;
    }
    public function metaDescription(): string
    {
    return trim((string) $this->get('summary'));
    }
    public function locale(): string
    {
    $languages = $this->wire()->languages;
    if ($languages && $this->wire()->user->language) {
    $name = (string) $this->wire()->user->language->name;
    return $name === 'default' ? 'en' : $name;
    }
    return 'en';
    }
    /**
    * @return array<string, mixed>
    */
    public function inertiaWithData(): array
    {
    return [
    'browserTitle' => $this->browserTitle(),
    'locale' => $this->locale(),
    ];
    }
    /**
    * @param array<string, mixed> $pageObject
    * @return array<string, mixed>
    */
    public function inertiaViewData(array $pageObject = []): array
    {
    $sanitizer = $this->wire()->sanitizer;
    $safeTitle = $sanitizer->entities($this->browserTitle());
    $safeDesc = $sanitizer->entities($this->metaDescription());
    return [
    'locale' => $this->locale(),
    'head' => [
    "<title>{$safeTitle}</title>",
    "<meta name=\"description\" content=\"{$safeDesc}\">",
    ],
    ];
    }
    }
  3. Create the template Page Class (site/classes/ArticlePage.php)

    Keep selector queries and deferred props inside the Custom Page Class so your template file stays minimal:

    <?php
    declare(strict_types=1);
    namespace ProcessWire;
    /**
    * @property string $title
    * @property string $summary
    * @property string $body
    * @property Pageimages $images
    */
    class ArticlePage extends DefaultPage
    {
    public function browserTitle(): string
    {
    return "{$this->title} | Journal";
    }
    /**
    * @return list<array<string, mixed>>
    */
    public function relatedArticles(int $limit = 4): array
    {
    $limit = max(1, $limit);
    $providers = $this->inertia()->providers();
    $items = [];
    foreach ($this->wire()->pages->find("template=article, id!={$this->id}, sort=-created, limit={$limit}") as $item) {
    if ($item instanceof Page) {
    $items[] = $providers->providePage($item);
    }
    }
    return $items;
    }
    /**
    * @return array<string, mixed>
    */
    public function inertiaWithData(): array
    {
    return [
    ...parent::inertiaWithData(),
    'relatedArticles' => $this->inertia()->defer(fn(): array => $this->relatedArticles()),
    ];
    }
    }
  4. Write the one-line template controller (site/templates/article.php)

    <?php
    declare(strict_types=1);
    namespace ProcessWire;
    /** @var ArticlePage $page */
    return $page->inertiaRender('Articles/Show');
  5. Verify your Root View (site/templates/_inertia.php)

    _inertia.php runs only on initial full-page loads:

    <?php
    declare(strict_types=1);
    namespace ProcessWire;
    if (!defined('PROCESSWIRE')) {
    die();
    }
    /**
    * @var Inertia $inertia
    * @var Sanitizer $sanitizer
    * @var array<string, mixed> $pageObject
    * @var string $locale
    * @var string $head
    */
    ?>
    <!DOCTYPE html>
    <html lang="<?= $sanitizer->entities((string) ($locale ?? 'en')) ?>">
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <?= $head ?? '' ?>
    <?php
    if (function_exists(__NAMESPACE__ . '\vite')) {
    echo vite([
    '@assets/css/app.css',
    '@assets/js/inertia.ts',
    ]);
    }
    ?>
    </head>
    <body>
    <?= $inertia->app($pageObject) ?>
    </body>
    </html>
  6. Create the frontend component (Articles/Show)

    import { Deferred, Head, Link } from '@inertiajs/react';
    import type { ArticlePageData } from '../types/inertia';
    interface Props {
    browserTitle: string;
    page: ArticlePageData;
    relatedArticles?: ArticlePageData[];
    }
    export default function Show({ browserTitle, page, relatedArticles }: Props) {
    return (
    <article>
    <Head title={browserTitle} />
    <h1>{page.title}</h1>
    <p>{page.summary}</p>
    <div dangerouslySetInnerHTML={{ __html: page.body }} />
    <section>
    <h2>Related Articles</h2>
    <Deferred data="relatedArticles" fallback={<p>Loading related articles...</p>}>
    <ul>
    {relatedArticles?.map((item) => (
    <li key={item.id}>
    <Link href={item.url}>{item.title}</Link>
    </li>
    ))}
    </ul>
    </Deferred>
    </section>
    </article>
    );
    }
  7. Generate TypeScript definitions

    Terminal window
    php vendor/bin/wire inertia:types --template=article

Next steps