what

Represent content as data

Move your future pages into one small data file, connect it to the site, and show a value the server read from it.

Allow 15–25 minutes. This is an editorial estimate. Continue in the same my-site folder.

Read

Start at L05. Your site has three fixed pages, a shared layout and a reusable card. So far every word lives inside an HTML file. A content site needs the same kind of entry many times: a title, a short summary and a body. Writing each one as its own page does not scale.

In this lesson the entries move into data/pages.json, a list of records. Each record is a set of named fields between braces. Every record has an id: a positive whole number that never changes and is never reused. The slug is the short, readable name a later lesson uses in its web address.

This file is read-only content. The server loads it, keeps each declared id, and refuses every attempt to change it through a form or upload. You edit content by editing the file. This is not the editable CMS yet; that comes later in the course.

Change

1. Add the data file. Create data/pages.json with this complete content. The records are made up; replace them with your own later.

data/pages.json
[
  {
    "id": 1,
    "title": "Why I started this website",
    "slug": "why-i-started",
    "summary": "A short note on what this site is for.",
    "body": "I wanted one small place on the web that is mine. This page explains why."
  },
  {
    "id": 2,
    "title": "What I am learning",
    "slug": "what-i-am-learning",
    "summary": "Notes from building pages by hand.",
    "body": "Every lesson adds one idea. This one is data: content that lives in a file instead of a page."
  },
  {
    "id": 3,
    "title": "Tools & ideas for later",
    "slug": "tools-and-ideas",
    "summary": "A short list of things to try next.",
    "body": "Cards, a table and one page per entry, all from the same file."
  }
]

Give every record an explicit id. The server also accepts records without one and numbers them by position, but those numbers change if you reorder the file, so the course always writes them out.

2. Connect it. Replace what.toml. The new [data] section at the end gives the file the name pages.

what.toml
# What Project Configuration

[server]
port = 8085
host = "127.0.0.1"

[session]
enabled = true
cookie_name = "w_session"
max_age = 604800
database = "data/sessions.db"

# Read-only content: the server loads this file and refuses every write to it.
[data]
pages = "data/pages.json"

Restart the dev server after changing what.toml: press Ctrl+C, then run run-what dev again. Changes to data/pages.json itself need no restart; the server compares the file's content on each request and loads it again when it changed.

3. Show one value. Create site/pages/index.html. The fetch.pages line reads the records into a variable named pages, and #pages|count# shows how many there are.

site/pages/index.html
<what>
title = "Pages | My website"
description = "Everything I have written, gathered from one data file."
fetch.pages = "local:pages"
</what>

<h1>Pages</h1>
<p>My data file holds <strong>#pages|count#</strong> pages so far.</p>
<p>The next lesson shows each one as a card.</p>

Check

  1. Open /pages. It should say “My data file holds 3 pages so far.” Home, About and Contact still work.
  2. Stop and start the dev server, then reload /pages. The count stays 3, and the records keep ids 1, 2 and 3.
  3. Open http://127.0.0.1:8085/data/pages.json. It should be “not found”: the raw file is never served to visitors.

Exercise

Add a fourth record with "id": 4 and its own slug, save, and reload /pages: the count becomes 4. Then give it "id": 3 instead. The terminal reports that two records share id 3, and the page keeps showing the last good content until you fix the file. Change it back to 4.

If something goes wrong

  • The count does not change. Look at the terminal. A missing comma or quote is reported with the file name and line number, and the site keeps serving the last good content until the file is valid again. run-what build refuses to build while the file is broken.
  • The count is empty. Check that what.toml has the [data] section, that you restarted the server, and that the page says local:pages.
  • Start again from a known state. Download the L06 checkpoint into a new, empty folder. It contains only this lesson's source files; never unpack it over your project.

Next: back to the course. The following lessons show these records as cards, as a table and as one page each.