Quickstart
This walkthrough shows how to render an article template through Inertia in Hybrid Mode while keeping classic ProcessWire templates untouched.
-
Enable the template for Inertia
Open Modules > Configure > Inertia, select Selected Templates Only (Hybrid Mode), and add
articleto Inertia Templates.You can also register templates programmatically in
site/init.phporsite/ready.php:<?phpdeclare(strict_types=1);namespace ProcessWire;inertia()->forTemplates(['article']); -
Add
InteractWithInertiatosite/classes/DefaultPage.phpDefaultPagedefines shared metadata (inertiaViewData()) and base props (inertiaWithData()) inherited by every template Page Class:<?phpdeclare(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}\">",],];}} -
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:
<?phpdeclare(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()),];}} -
Write the one-line template controller (
site/templates/article.php)<?phpdeclare(strict_types=1);namespace ProcessWire;/** @var ArticlePage $page */return $page->inertiaRender('Articles/Show'); -
Verify your Root View (
site/templates/_inertia.php)_inertia.phpruns only on initial full-page loads:<?phpdeclare(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 ?? '' ?><?phpif (function_exists(__NAMESPACE__ . '\vite')) {echo vite(['@assets/css/app.css','@assets/js/inertia.ts',]);}?></head><body><?= $inertia->app($pageObject) ?></body></html> -
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>);}<script setup lang="ts">import { Deferred, Head, Link } from '@inertiajs/vue3';import type { ArticlePageData } from '../types/inertia';defineProps<{browserTitle: string;page: ArticlePageData;relatedArticles?: ArticlePageData[];}>();</script><template><article><Head :title="browserTitle" /><h1>{{ page.title }}</h1><p>{{ page.summary }}</p><div v-html="page.body" /><section><h2>Related Articles</h2><Deferred data="relatedArticles"><template #fallback><p>Loading related articles...</p></template><ul><li v-for="item in relatedArticles" :key="item.id"><Link :href="item.url">{{ item.title }}</Link></li></ul></Deferred></section></article></template> -
Generate TypeScript definitions
Terminal window php vendor/bin/wire inertia:types --template=article