Build a View
Walkthrough part 1: scaffold an app and build a live, searchable view from one .pzl file.
Over this page and the next two you'll build a small spellbook: a searchable list of spells, a detail page for each one, and a schema-backed model powering both. Each part exercises one subsystem: views here, then the router, then models. If you haven't read App Structure, skim it first; this page assumes its vocabulary.
Scaffold the app
npm install -g @magic-spells/puzzle puzzle init spellbook cd spellbook && npm install npm run dev
Open the dev server URL it prints. You have a running app with one home view, and the next step replaces it with ours.
The spellbook view
Create app/views/Spellbook.pzl. It holds the spell data as a plain array for now (part 3 moves it into the store), plus a search box and the list:
<puzzle-view class="spellbook">
<h1>Spellbook</h1>
<input type="search" placeholder="Search spells…" value={ query } />
<p>{ pluralize(spells.length, 'spell') }</p>
{#if spells.length}
<ul>
{#for spell in spells}
<li key={ spell.id }>
<strong>{ spell.name }</strong>
<span>{ spell.school } · level { spell.level }</span>
</li>
{/for}
</ul>
{:else}
<p>No spells match “{ query }”.</p>
{/if}
</puzzle-view>
<script>
import { PuzzleView } from '@magic-spells/puzzle';
const SPELLS = [
{ id: 's1', name: 'Featherfall', school: 'Transmutation', level: 1 },
{ id: 's2', name: 'Mage Armor', school: 'Abjuration', level: 1 },
{ id: 's3', name: 'Misty Step', school: 'Conjuration', level: 2 },
{ id: 's4', name: 'Counterspell', school: 'Abjuration', level: 3 },
{ id: 's5', name: 'Polymorph', school: 'Transmutation', level: 4 },
];
export default class Spellbook extends PuzzleView {
data(params, props) {
const query = this.getData()?.query ?? '';
const needle = query.toLowerCase();
return {
query,
spells: SPELLS.filter((s) => s.name.toLowerCase().includes(needle)),
};
}
}
</script>Point the scaffolded route at it. app/routes.js already exists: puzzle init generated it with a home route and a catch-all. Import your view and swap it in on the path: '/' entry:
// app/routes.js: the scaffold created this; swap the home view for yours
import Spellbook from './views/Spellbook.pzl';
import NotFoundView from './views/NotFound.pzl';
import DefaultLayout from './layouts/Default.pzl';
export default [
{
path: '/',
name: 'home',
view: Spellbook, // was HomeView
layout: DefaultLayout,
meta: { title: 'Spellbook' },
},
{ path: '*', name: 'not-found', view: NotFoundView, layout: DefaultLayout, meta: { title: 'Not found' } },
];Save, and the dev server reloads with your spellbook. Routes get a proper introduction in part 2; for now, all that changed is which view the home URL renders.
How the search works
There is no input handler anywhere in that file, yet typing filters the list live. Two framework behaviors combine to make that happen:
- Form controls bind themselves.
value={ query }is a two-way binding because its expression is a bare data key on a plain input with no author handler. Typing writesqueryback into the view's local state. - A bound bare key re-runs
data(). That's the special part: because derived values like the filtered list live indata(), the framework recomputes it on every keystroke, and the template re-reads the result.
Follow one keystroke through: the input writes query, data() re-runs, it reads the fresh query via this.getData(), filters SPELLS, and returns a new spells list, and the list patches. One direction, no glue code.
Reading the template
{#for spell in spells}loops rows;key={ spell.id }on the row root lets the framework track rows by identity instead of position when the list reorders or filters.{#if}/{:else}renders a fallback when the filter matches nothing. Conditionals are template blocks, not JavaScript.{ pluralize(spells.length, 'spell') }calls a function from the template library, a display-only transform.pluralizeprints the count and the word together:1 spell,5 spells.spells.lengthis the list's count, as in JavaScript. A template expression is JavaScript-shaped, so the filter could be written in the template withspells.filter(s => …); keeping it indata()gives the template one finished list to read, and it's where part 3's store query goes.- Interpolations like
{ spell.name }become text nodes, so a value can't inject markup. Text is text.
What you can't do yet
Try to add a "mark as learned" button and the shape of the problem appears: the spells live in a module-level array, so there's no schema-validated write path, no store-managed lookup, and nothing that would tell other views a spell changed. You could mutate the array and call refresh(), but that's state management by hand, exactly what the store exists to replace. Part 3 fixes this properly; first, the app needs more than one page.
Next: Wire the Router, with a layout, a detail page, and navigation between them.