Quick Start
Build a working page from scratch: a view, a route, and the app entry point.
This walks through the smallest complete Puzzle app: an HTML shell, an app entry point, one route, and one view. If you scaffolded with puzzle init you already have all of this, so read on to understand what each piece does.
Project layout
A Puzzle app lives under an app/ directory:
my-app/
├── puzzle.config.js # build config (styles pipeline, output mode)
└── app/
├── app.js # app initialization
├── routes.js # route table
├── public/
│ └── index.html # the HTML shell (#app mount + module script)
├── styles/
│ └── styles.css # global stylesheet / Tailwind entry
├── assets/ # compile-time inputs, such as {#svg} icons
├── layouts/ # chrome around pages (.pzl)
├── views/ # page components (.pzl)
├── components/ # reusable UI (.pzl)
└── models/ # data models (.js)1. The HTML shell
app/public/index.html is the page the browser loads. It needs an empty mount element and a module script pointing at the compiled bundle:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My App</title> <link href="/styles.css?v=01beceeb43" rel="stylesheet"> </head> <body> <div id="app"></div> <script type="module" src="/app.js"></script> </body> </html>
2. A view
Views are page components. Create app/views/Home.pzl:
<puzzle-view class="home">
<h1>{ title }</h1>
<button @click={ cheer }>Cheer</button>
<p>{ pluralize(cheers, 'cheer') } so far.</p>
</puzzle-view>
<script>
import { PuzzleView } from '@magic-spells/puzzle';
export default class HomeView extends PuzzleView {
created() {
this.setData({ cheers: 0 });
}
data(params, props) {
return {
title: 'Hello, Puzzle',
cheers: this.getData().cheers,
};
}
events = {
cheer: () => {
this.setData('cheers', this.getData().cheers + 1);
this.refresh();
},
};
}
</script>Two things worth naming now:
data(params, props)returns the component's model, the object the template reads from. It runs when the component mounts and again whenever store data it queried changes.setData()updates local state and re-renders, but does not re-rundata(). When a value shown in the template is derived insidedata(), ascheersis here, callthis.refresh()aftersetData()to recompute it.
3. A layout
Layouts wrap views with shared chrome. The routed view renders wherever you place a single <Slot/>. Create app/layouts/Default.pzl:
<puzzle-view class="shell">
<header><h1>My App</h1></header>
<main>
<Slot/>
</main>
<footer>Made with Puzzle</footer>
</puzzle-view>
<script>
import { PuzzleView } from '@magic-spells/puzzle';
export default class DefaultLayout extends PuzzleView {}
</script>4. The route table
app/routes.js exports an array of route objects. Views and layouts are imported at the top, or loaded on demand with lazy() (see Routing):
import HomeView from '@/views/Home.pzl';
import DefaultLayout from '@/layouts/Default.pzl';
export default [
{
path: '/',
name: 'home',
view: HomeView,
layout: DefaultLayout,
meta: { title: 'Home · My App' },
},
];5. The app entry
app/app.js creates the app and mounts it:
import { PuzzleApp } from '@magic-spells/puzzle';
import routes from '@/routes.js';
const app = new PuzzleApp({
target: '#app',
routes,
});
app.mount();
// Exporting the app is required for prerendered builds; see Static Builds.
export default app;The core config is small: target, routes, models, formatters, and apiURL. Optional keys add server sync (an adapter imported from @magic-spells/puzzle/adapter), router settings, lifecycle hooks, and error handling; they're covered in Routing, Data & Models, and Components.
6. Run it
npm run dev
puzzle dev watches app/, rebuilds on change, and reloads the browser with your state intact. Edit Home.pzl, save, and the page updates.
Where to go next
- Components: the
.pzlfile in depth. - Templates: the full template grammar.
- Data & Models: give your app real data.
- Routing: add more pages and navigation.