One Card, Many Posts: Render Lists Without Repeating Code
You've got a page with twelve blog cards on it, and every card is the same block of HTML copied and pasted with a different title. Now someone asks you to add the author's name to every card.
Today I'll show you how to write a card once and let the data fill it in, in plain JavaScript first and then in React and Next.js, so a change like that is one edit instead of twelve.
Here's the short answer, so you have it before anything else. Put the things that change, like the title, the text, the image and the date, in a list of data. Write the markup for one card once, as a function or a component. Then loop over the list and call that function for every item. Change the function and every card changes with it.
That's the whole pattern behind every blog list, product grid and feed you've ever scrolled.
If you're a beginner, read this top to bottom, and click the live demo in the middle, because seeing it is worth more than my words. If you already loop over arrays every day, jump to the section about innerHTML, and then the one about keys in React. Those two are where people who know the basics still get hurt. The last sections, on missing data, dates, big lists and code an AI wrote for you, are for those of you who review other people's code.
The problem: twelve copies of the same card
This is how almost everyone starts. You build one card, it looks good, so you copy it. Then you copy it again. Here are three of them, and in a real page there would be twelve.
index.html
<div class = "grid" >
<article class = "card" >
<img src = "/images/css.webp" alt = "A stylesheet open next to a web page" width = "320" height = "180" >
<h2> First steps with CSS </h2>
<p> Colors, spacing and fonts, explained from zero. </p>
<time datetime = "2026-10-12" > Oct 12, 2026 </time>
</article>
<article class = "card" >
<img src = "/images/forms.webp" alt = "A sign-up form with two fields" width = "320" height = "180" >
<h2> How forms work </h2>
<p> What really happens when you press submit. </p>
<time datetime = "2026-10-19" > Oct 19, 2026 </time>
</article>
<article class = "card" >
<img src = "/images/errors.webp" alt = "A red error message in the browser console" width = "320" height = "180" >
<h2> Reading error messages </h2>
<p> The red text is trying to help you. </p>
<time datetime = "2026-10-26" > Oct 26, 2026 </time>
</article>
</div>
Look at what's actually different between those blocks. The image file, the alt text, the title, one sentence and a date. Everything else, the article tag, the class names, the order of the elements, the width and height of the image, is identical.
You typed the same structure three times to change five small values. And it works. That's the trap. Nothing is broken on the day you write it. It breaks on the day something changes.
Someone asks for the author's name under the title, and now you open the file and make the same edit twelve times. You'll miss one. You'll put the name above the title in card seven because you were tired. Or a new post comes out and you copy the block again, and the new card quietly has last month's date because you forgot to change it.
Here's the thing that annoys me about how this gets taught. Courses love exercises like pulling a name out of an array, and you do it, and you have no idea where that shows up in a real website. This is where it shows up. A list of blog posts is an array. A card is what you do with one item of it. I wrote more about that gap in why courses leave you feeling like you know nothing , and this article is one of the places where the pieces finally connect.
Why HTML makes you repeat yourself
HTML isn't a programming language. It's a description of a document: here's a heading, here's a paragraph, here's an image. It has no way to say "do this for every post". There's no loop in HTML, no variables, no functions.
If you've only written HTML so far, like in building your first web page , copying is the only tool you have. So you're not doing anything wrong. You've just reached the edge of what HTML can do by itself.
To go past that edge you need something that can repeat things for you. In the browser that's JavaScript. On a server it could be any language: PHP, Python, JavaScript again in Node.js. The idea is the same everywhere, which is why it's worth learning properly once.
Think about wedding invitations. Nobody designs two hundred invitations. Somebody designs one card, with a blank where the guest's name goes. Then there's a guest list. And then for every name on the list, somebody prints the card and writes that name in the blank. One design, one list, one repeated action. If the date of the wedding changes, you fix the design once, and every invitation printed after that is right.
Your blog cards are exactly that. The card design is the template. The guest list is your data. Printing one card per guest is the loop.
The idea: separate the data from the template
So we split the page into three parts, and each part has one job.
- The data: a list of posts, where each post is a small bundle of values. Title, excerpt, date, image, alt text. This is the only part that changes when you publish something new.
- The template: the markup of one card, written once, with blanks where the values go.
- The loop: a few lines that walk through the data and fill in the template for each item.
Let's start with the data. In JavaScript, a list is an array and a bundle of named values is an object. So a list of posts is an array of objects.
posts.js
const posts = [ { slug : "first-steps-with-css" , title : "First steps with CSS" , excerpt : "Colors, spacing and fonts, explained from zero." , date : "2026-10-12" , image : "/images/css.webp" , imageAlt : "A stylesheet open next to a web page" , } , { slug : "how-forms-work" , title : "How forms work" , excerpt : "What really happens when you press submit." , date : "2026-10-19" , image : "/images/forms.webp" , imageAlt : "A sign-up form with two fields" , } , ] ;
Let's go through it. The square brackets make an array, which is just a list with an order. Inside it, each pair of curly braces is one object, which is one post. Inside the object, every line is a name and a value: slug is "first-steps-with-css", title is "First steps with CSS", and so on. The comma between the two objects separates the posts.
Why is there a slug? It's the post's unique name, the part that ends up in the address bar. We don't show it on the card right now, but we'll need something that's different for every post later, when React asks for keys. Give every item in a list a unique ID from the start and you'll thank yourself.
Why is the date written as "2026-10-12" and not as "Oct 12, 2026"? Because the data should store the fact, and the template should decide how to show it. That year-month-day format is unambiguous and it sorts correctly as plain text. How it looks to a reader is a display decision, and we'll make it in one place.
And notice that imageAlt is its own field. Alt text describes the image for people who can't see it. It belongs to each post, because every image is different. If you skip it here, there's nowhere sensible to add it later.
Note: Terms in plain words.
- An array is an ordered list of values.
- An object is a bundle of named values, like a form someone filled in.
- A function is a named piece of code you can run again and again with different inputs.
- A loop is code that repeats something once for every item in a list.
- The DOM is the browser's live version of your HTML, the thing JavaScript can change after the page has loaded.
Step 1: write the card once, as a function
Now the template. We write a function that takes one post and gives back one finished card. Every value from the post goes into the right place, and the structure is written exactly once.
render-dom.js
const dateFormat = new Intl . DateTimeFormat ( "en-US" , { dateStyle : "medium" , timeZone : "UTC" } ) ; function createCard ( post ) { const card = document . createElement ( "article" ) ; card . className = "card" ; const img = document . createElement ( "img" ) ; img . src = post . image ; img . alt = post . imageAlt ; img . width = 320 ; img . height = 180 ; const title = document . createElement ( "h2" ) ; title . textContent = post . title ; const excerpt = document . createElement ( "p" ) ; excerpt . textContent = post . excerpt ; const time = document . createElement ( "time" ) ; time . dateTime = post . date ; time . textContent = dateFormat . format ( new Date ( post . date ) ) ; card . append ( img , title , excerpt , time ) ; return card ; }
Line by line, because every line is there for a reason.
The first line creates a date formatter. Intl.DateTimeFormat is built into every modern browser and into Node.js, and it turns a date into text for a given language. "en-US" with dateStyle "medium" gives you "Oct 12, 2026". We create it once, outside the function, because there's no reason to build a new formatter for every single card. The timeZone part looks odd, and I'll explain it properly in the section about dates, because it fixes a real bug.
Then function createCard(post). This is our template. It takes one post, the one object, and the word post is just the name we give it inside the function. document.createElement("article") makes a new article element. It isn't on the page yet. It only exists in memory, like a card on your desk that hasn't been put in the envelope. Then we give it the class name "card" so the CSS can style it.
Next comes the image. We set src from post.image and alt from post.imageAlt, so each card gets its own picture and its own description. The width and height are the same for every card, which is why they're written here and not in the data. They tell the browser how much space to reserve before the image downloads, so the page doesn't jump around while it loads.
The title, the excerpt and the time all follow the same move: create the element, then put text into it with textContent. Remember that word. textContent means "treat this as text, never as HTML". It matters a lot, and you'll see exactly why in a minute.
The time element gets two things. dateTime holds the machine-readable date, the "2026-10-12" from the data, so browsers, search engines and screen readers know what it means. textContent holds the human version, "Oct 12, 2026". Same fact, two audiences.
Finally, card.append adds all four children to the article, in that order, and return card hands the finished card back to whoever called the function. That's the whole template. If you want the author's name under the title tomorrow, you add two lines here and every card on the page gets it.
Step 2: loop over the data and put the cards on the page
We have data and we have a template. The loop is the shortest part.
mount.js
const list = document . querySelector ( "#posts" ) ; const fragment = document . createDocumentFragment ( ) ; for ( const post of posts ) { fragment . append ( createCard ( post ) ) ; } list . replaceChildren ( fragment ) ;
The first line finds the container on the page, the element with the id "posts". That's an empty div you put in your HTML where the cards should appear.
Then we create a DocumentFragment . Think of it as a tray. Instead of carrying each card to the page one at a time, you put them all on the tray and carry the tray over once. The page gets updated in one go.
for (const post of posts) is the loop. Read it out loud: for each post of the posts. On every round, post is the next object from the array, and we pass it to createCard and drop the result on the tray.
The last line, list.replaceChildren(fragment), clears whatever was inside the container and puts the cards there. Using replace instead of append means you can run this again later, after sorting or filtering, without ending up with the old cards and the new cards both on the page.
Tip: Honest note on the tray. For ten or twenty cards you will not see any difference between appending each card directly and using a fragment. I use it because it's the same amount of code and it's the habit that holds up when the list gets long. If you appended one by one and it works, you didn't write a bug.
That's three files and maybe forty lines of JavaScript, and it doesn't matter anymore whether you have three posts or three hundred. The page you write stays the same size. Only the data grows.
See it run: one function, any number of cards
Here it is live. Every card below comes from one createCard function and a small array. Click Add a post and watch a new card appear, with no new HTML written. Click Oldest first to sort the same data the other way. Empty the list to see what happens when there's nothing to show. Leave the checkbox alone for now. It's for the next section.
Live demo: Card list from data. It runs right in the page, so try it in the original article . The colored blocks stand in for images, because the demo can't load pictures. In a real page that's the img element from Step 1.
Notice what sorting did. We didn't move any HTML around. We sorted the data and drew the cards again. That's a habit worth building early: change the data, then render again. Don't reach into the page and shuffle elements by hand. When the data is the truth and the page is just a picture of the data, a whole class of bugs disappears, because the picture can never drift away from the truth.
The shortcut everyone writes, and why it bites: innerHTML
If you search for how to render a list, or ask an AI to do it, there's a good chance you'll get something like this instead of createElement. It's shorter and it looks like HTML, which feels nicer.
render-string.js
function cardHTML ( post ) { return ` <article class="card"> <img src="${post.image}" alt="${post.imageAlt}" width="320" height="180"> <h2>${post.title}</h2> <p>${post.excerpt}</p> <time datetime="${post.date}">${post.date}</time> </article>` ; } document . querySelector ( "#posts" ) . innerHTML = posts . map ( cardHTML ) . join ( "" ) ;
Here's what it does. cardHT
Comments
No comments yet. Start the discussion.