Subscribe

Astro recipe collection website - Part 2 Homepage rendering

โœ๏ธ

Adding the five last recipes to our homepage using Astro collections

13 Aug, 2021 ยท 3 min read

In this series, we are building a recipe website in Astro. We already made a start by setting up our collections.

Today weโ€™ll learn how to render the five latest articles on the homepage.

Showing part of the collection on homepage Astro

Adding a collection to our homepage

So far, we have been using the default homepage that Astro came with. We want to change that to showcase the last five recipes dynamically.

Note: Where dynamically means on every build.

To get started with this, letโ€™s clean up the homepage. This way, we will have a blank canvas to begin with.

---
const title = 'Astro Recipe Website';
---
<!-- HTML Code -->
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width">
    <title>{title}</title>
    <link rel="icon" type="image/svg+xml" href="/favicon.svg">
    <link rel="stylesheet" href="/style/global.css">
</head>
<body>

</body>
</html>

As you can see, we have a section of Frontmatter again. This is the section between the three dashes where we defined the title.

In this section, we will also load the five last recipes.

---
const title = 'Astro Recipe Website';
const recipes = Astro.fetchContent('./recipe/*.md')
    .sort((a, b) => a.date.localeCompare(b.date)).slice(0, 5);
---

We use the same method of loading the content by leveraging the fetchContent ability of Astro. Then we sort the articles based on their date and slice the array only to return five items.

Then we want to loop through these results and render an article for each recipe.

<section>
  {recipes.map(recipe => (
  <article>
    <a href="{recipe.url}">
      <img src="{recipe.image}" alt="{recipe.title}" />
      <h3>{recipe.title}</h3>
    </a>
  </article>
  ))}
</section>

This will render a section with our five recipes and show the image and a heading with the title. When we click the card, it takes us to that recipe.

Letโ€™s also add some very basic CSS to look a bit better. The following code can be placed in the head of your index.astro file.

<style lang="scss">
    section {
        display: grid;
        grid-template: '1fr 1fr 1fr 1fr 1fr';
    }
    article {
        padding: 1rem;
        img {
            object-fit: cover;
            width: 100%;
            height: 200px;
            margin-bottom: 0.5rem;
        }
        h3 {
            color: #fff;
        }
    }
</style>

You can find the complete code on GitHub.

Thank you for reading, and letโ€™s connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next ๐Ÿ“–

Computed Nano Stores

6 Sep, 2022 ยท 2 min read

Computed Nano Stores

Astro Nano Stores maps

5 Sep, 2022 ยท 3 min read

Astro Nano Stores maps

Join 2099 devs and subscribe to my newsletter