what

Turn content into cards

Show every record from your data file as a card, with one loop and the card component you already have.

Allow 15–25 minutes. This is an editorial estimate. Continue in the same my-site folder. Keep the dev server running; nothing in this lesson needs a restart.

Read

Start at L06. /pages shows how many records data/pages.json holds. Now each record becomes a card. A loop repeats its content once per record; inside it, #page.title# is the current record's title. Add a record to the file and a card appears; remove one and it goes. Cards are listed by id, from lowest to highest. A record without a summary still gets its card, just without that line.

A condition shows content only when a value is present. The card's link becomes conditional, because these pages have no page of their own yet: a link to an address that does not exist would only lead visitors to “not found.” The cards on Home keep their links, because they still pass an href.

Change

1. Make the summary and link optional. Replace components/content-card.html. <if summary> and <if href> render their content only when the caller supplied that value.

components/content-card.html
<what>
props = "heading, href, summary, label"
defaults.label = "Read more"
</what>

<article class="content-card">
  <h2>#heading#</h2>
  <if summary><p>#summary#</p></if>
  <slot/>
  <if href><a href="#href#">#label#<span class="visually-hidden">: #heading#</span></a></if>
</article>

2. Loop over the records. Replace site/pages/index.html. <if pages> is true when the list has at least one record; <else/> shows a friendly message when it is empty.

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>
<if pages>
<div class="card-grid">
<loop data="#pages#" as="page">
  <what-content-card heading="#page.title#" summary="#page.summary#"/>
</loop>
</div>
<else/>
<p class="empty-state">No pages yet. Add a record to data/pages.json.</p>
</if>

The loop reads the list once and passes each record's fields to the card. Values are inserted as text, so the ampersand in “Tools & ideas for later” appears as written instead of being read as HTML.

Check

  1. Open /pages. Three cards appear in ascending id order, whatever the order in the file, each with a heading and a summary and no link.
  2. Open Home. Its two cards still link to About and Contact.
  3. Temporarily change data/pages.json to [] and reload: the page says “No pages yet.” Put the records back.

Exercise

Add "featured": true to one record. In the loop, pass it to the card's slot with <if page.featured><p class="card-note">Featured</p></if> between an opening and a closing <what-content-card> tag. Only that card shows the note.

If something goes wrong

  • A card shows a raw #page.title#. Check that the loop says as="page" and that the field name matches the data file exactly.
  • No cards and no message. Look at the terminal for a data file error, and check the fetch.pages line.
  • Start again from a known state. Download the L07 checkpoint into a new, empty folder; never unpack it over your project.

Next: show the same records as a table.