what

Show the same records as a table

Add a read-only inventory page that lists the same records as rows, from the same data file.

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

Read

Start at L07. The cards show records for reading. A table suits a different job: comparing the same fields across every record. Both pages read the same source, data/pages.json, in the same id order; there is no second copy to keep in step. A page you opened before an edit still shows the old version until it reloads.

This page is public and read-only. It is not the place where content is edited; a protected editing area comes later in the course, and it will not be this page.

Change

1. Add the page. Create site/inventory.html. The caption names the table for screen readers, and each column has a header.

site/inventory.html
<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>#page.title#</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>

2. Style the table. Replace static/styles.css. The new rules at the end let a wide table scroll inside its own box on a narrow screen instead of widening the whole page, and style the empty message.

static/styles.css
/* Framework styles are supplied automatically. These rules style our site. */
.site-body {
  margin: 0;
  color: #26372c;
  background: #faf8f2;
  font-family: system-ui, sans-serif;
  line-height: 1.65;
}
.site-header, .site-main, .site-footer {
  width: min(100% - 2rem, 48rem);
  margin-inline: auto;
}
.site-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding-block: 1.5rem;
  border-bottom: 1px solid #c9cec4;
}
.site-name { font-size: 1.2rem; font-weight: 750; }
.site-nav { display: flex; flex-wrap: wrap; gap: 1rem; }
.site-body a { color: #315f40; text-underline-offset: 0.2em; }
.site-body a:focus-visible {
  outline: 3px solid #825414;
  outline-offset: 4px;
}
.site-main { padding-block: 2.5rem; }
.site-main h1 { font-size: clamp(2rem, 7vw, 3rem); line-height: 1.15; }
.site-main p { max-width: 65ch; }
.site-main figure { margin: 2rem 0; }
.site-image { display: block; max-width: 100%; height: auto; border-radius: 1rem; }
.site-main figcaption { margin-top: 0.6rem; color: #4b594e; }
.site-footer { border-top: 1px solid #c9cec4; padding-block: 1.5rem; }
.skip-link {
  position: absolute;
  top: -10rem;
  left: 1rem;
  padding: 0.6rem 1rem;
  background: #fff;
  z-index: 10;
}
.skip-link:focus { top: 1rem; }

.card-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 18rem), 1fr)); gap: 1.25rem; }
.content-card { padding: 1.5rem; border: 1px solid #c9cec4; border-radius: 1rem; background: #fffdf8; }
.content-card h2 { margin-top: 0; font-size: 1.4rem; }
.card-note { font-size: 0.9rem; color: #4b594e; }
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.empty-state { padding: 1rem 1.25rem; border: 1px dashed #c9cec4; border-radius: 1rem; }
.table-wrap { overflow-x: auto; }
.inventory { border-collapse: collapse; width: 100%; min-width: 28rem; }
.inventory caption { text-align: left; padding-bottom: 0.6rem; color: #4b594e; }
.inventory th, .inventory td { text-align: left; padding: 0.6rem 0.8rem; border-bottom: 1px solid #c9cec4; vertical-align: top; }
.inventory thead th { border-bottom-width: 2px; }

Check

  1. Open /inventory. The table lists ids 1, 2 and 3 with the same titles as the cards on /pages.
  2. Make the window narrow, or use a phone. The page itself does not scroll sideways; only the table may.
  3. Set data/pages.json to []: both /pages and /inventory say “No pages yet.” Put the records back.

Exercise

Add a Summary column: one more <th scope="col"> in the header and one more <td>#page.summary#</td> in the row. Use the same data, not a second file.

If something goes wrong

  • The table and the cards differ. Reload both pages first. Both must say fetch.pages = "local:pages"; there is only one source.
  • The page scrolls sideways on a phone. Check that the table sits inside <div class="table-wrap"> and that the stylesheet was replaced.
  • Start again from a known state. Download the L08 checkpoint into a new, empty folder; never unpack it over your project.

Next: back to the course. One page per record, with its own address, comes in a later lesson.