Wire the Router
Walkthrough part 2: add a layout, a second page, params, and navigation between them.
Part 1 left the spellbook as a single view with its data trapped in the file. This part gives the app real structure: shared data both pages can read, a layout that wraps every page, and a detail route with a URL parameter.
Add a layout
A layout is chrome that persists across pages. The routed view renders at the <Slot/> marker. Replace the scaffold's app/layouts/Default.pzl with:
<puzzle-view class="shell">
<header>
<a href="{ link('/') }">Spellbook</a>
</header>
<main>
<Slot/>
</main>
</puzzle-view>
<script>
import { PuzzleView } from '@magic-spells/puzzle';
export default class DefaultLayout extends PuzzleView {}
</script>Navigation is a plain <a href>. The link() function makes the href portable across router modes: it emits a normal path in path mode and a hash href in hash mode, from the same template.
The detail view
Create app/views/SpellDetail.pzl. It reads which spell to show from the URL via params.id:
<puzzle-view class="spell-detail">
{#if spell}
<h1>{ spell.name }</h1>
<p>{ spell.school } · level { spell.level }</p>
{:else}
<h1>Unknown spell</h1>
{/if}
<a href="{ link('/') }">← All spells</a>
</puzzle-view>
<script>
import { PuzzleView } from '@magic-spells/puzzle';
import { SPELLS } from '@/lib/spells.js';
export default class SpellDetail extends PuzzleView {
data(params, props) {
return {
spell: SPELLS.find((s) => s.id === params.id) ?? null,
};
}
}
</script>Route params always arrive as strings: params.id here is 's3', not a number. That's fine for our string ids, and when the store takes over in part 3 it won't matter at all, because record lookup treats 7 and '7' as the same key.
The route table
Replace app/routes.js with three routes: list, detail, and a catch-all:
import Spellbook from '@/views/Spellbook.pzl';
import SpellDetail from '@/views/SpellDetail.pzl';
import NotFound from '@/views/NotFound.pzl';
import DefaultLayout from '@/layouts/Default.pzl';
export default [
{
path: '/',
name: 'spellbook',
view: Spellbook,
layout: DefaultLayout,
meta: { title: 'Spellbook' },
},
{
path: '/spells/:id',
name: 'spell-detail',
view: SpellDetail,
layout: DefaultLayout,
meta: { title: 'Spell · Spellbook' },
},
{
path: '*',
name: 'not-found',
view: NotFound,
layout: DefaultLayout,
meta: { title: 'Not found · Spellbook' },
},
];Worth naming as you read it:
- The route table is pure data. Views here are statically imported; for a view you'd rather download on demand,
lazy()wraps the import (see Routing). :idcaptures a param. Routes match in order, and the*catch-all always matches last.meta.titlesets the document title per route, inherited leaf to root and synced on every navigation.
The catch-all needs a view to render. The scaffold already created app/views/NotFound.pzl, with a heading and a link home; it's the page every unmatched URL lands on.
Link the list
Back in Spellbook.pzl, wrap each row's name in a link to its detail page:
{#for spell in spells}
<li key={ spell.id }>
<a href="{ link('/spells/' + spell.id) }"><strong>{ spell.name }</strong></a>
<span>{ spell.school } · level { spell.level }</span>
</li>
{/for}Click through and use the back button. Notice what you did not write: the router intercepted the link, swapped the view inside the layout, saved and restored scroll, moved focus to the incoming page, and announced the new title to assistive tech. Deep-link a detail URL directly and it renders the right spell from a cold load, because data(params) reads params on first render like any other.
One caveat to file away
If you later prerender your app to static HTML, a :param route like /spells/:id is prerendered once per staticPaths entry; without one it is skipped with a warning. It doesn't affect this walkthrough's SPA, but it's the first thing to revisit in Static Builds when you get there.
Next: Add Models, the payoff: schema-backed data and views that react on their own.