Use one template for every content page
Give every record its own address, /pages/<slug>, rendered by one template file, and answer “not found” when no record matches.
Allow 15–25 minutes. This is an editorial estimate. Continue in the same my-site folder with the dev server running.
Read
Start at L08. The cards and the table list every record, but no record has a page of its own yet. Writing one HTML file per record would bring back the problem the data file solved. Instead, one template serves them all.
A file named with square brackets, site/pages/[slug].html, answers any address of the form /pages/something. The part after /pages/ is the route value; inside the page it is #params.slug#, named after the brackets. Real files still win: /pages keeps showing site/pages/index.html.
The page then looks up one record. fetch.page.single = true asks for exactly one: when no record has that slug, the visitor gets the site’s “not found” page with a real 404 status, never an empty page. If two records share a slug, the page refuses to guess and reports an error, so keep every slug unique.
Change
1. Add the content component. Create components/page-content.html. It shows one record: its title as the page heading, the summary when there is one, and the body.
<what>
props = "heading, summary, body"
</what>
<article class="page-content">
<h1>#heading#</h1>
<if summary><p class="page-summary">#summary#</p></if>
<p>#body#</p>
</article>
2. Add the template. Create site/pages/[slug].html, with the square brackets in the file name. The filter line finds the record whose slug equals the route value, and single = true asks for exactly one.
<what>
title = "Page | My website"
description = "One page from the data file."
fetch.page = "local:pages"
fetch.page.filter = "slug=#params.slug#"
fetch.page.single = true
</what>
<what-page-content heading="#page.title#" summary="#page.summary#" body="#page.body#"/>
<p><a href="/pages">All pages</a></p>
3. Link the cards. Replace site/pages/index.html. Each card now passes an href, so the card shows its “Read more” link. The |urlencode filter turns the slug into one safe piece of an address: a slug with a space, ? or # still links to its own page.
<what>
title = "Pages | My website"
description = "Everything I have written, gathered from one data file."
fetch.pages = "local:pages"
</what>
<h1>Pages</h1>
<if pages>
<div class="card-grid">
<loop data="#pages#" as="page">
<what-content-card heading="#page.title#" summary="#page.summary#" href="/pages/#page.slug|urlencode#"/>
</loop>
</div>
<else/>
<p class="empty-state">No pages yet. Add a record to data/pages.json.</p>
</if>
4. Link the table. Replace site/inventory.html. Each title becomes a link to its page, built the same way.
<what>
title = "Inventory | My website"
description = "Every page in the data file, one row each."
fetch.pages = "local:pages"
</what>
<h1>Inventory</h1>
<p>The same records as the Pages list, shown as a table. This is a public list for visitors, not an admin screen: nothing here can change the data.</p>
<if pages>
<div class="table-wrap">
<table class="inventory">
<caption>Pages in data/pages.json</caption>
<thead>
<tr><th scope="col">Id</th><th scope="col">Title</th><th scope="col">Slug</th></tr>
</thead>
<tbody>
<loop data="#pages#" as="page">
<tr><td>#page.id#</td><td><a href="/pages/#page.slug|urlencode#">#page.title#</a></td><td><code>#page.slug#</code></td></tr>
</loop>
</tbody>
</table>
</div>
<else/>
<p class="empty-state">No pages yet. Add a record to data/pages.json.</p>
</if>
No restart is needed: new pages and components are picked up while the server runs.
Check
- Open /pages and follow “Read more” on the first card. The address is
/pages/why-i-startedand the page shows that record’s title, summary and body. - Follow a title link in /inventory: it opens the same page.
- Open
http://127.0.0.1:8085/pages/no-such-page. You get the “not found” page, and the browser’s network panel shows status 404. /pagesitself still shows the cards: a real file always wins over the template.
Exercise
Add a fourth record to data/pages.json with "id": 4 and the slug "my-fourth-page". Without adding any HTML file, open /pages/my-fourth-page: the template already serves it, and its card and table row link to it.
If something goes wrong
- Every address under /pages shows “not found”. Check the file name: it must be exactly
[slug].html, lowercase, insidesite/pages/. The name between the brackets must match#params.slug#. - One page shows an error. Two records probably share that slug. Give each record its own slug.
- The static export has no record pages. That is expected. Pages served by a template need the running server; the export from lesson 5 stays an export of the fixed pages.
- Start again from a known state. Download the L09 checkpoint into a new, empty folder; never unpack it over your project.