#how to get collection name

3 messages · Page 1 of 1 (latest)

solemn badger
#

How can a component get the current page's collection name?

polar terraceBOT
#

Hi @solemn badger. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To get the current page's collection name in an Astro component, you can access it through the entry property of Astro.props. Here's how you can do it:

---
const { entry } = Astro.props;
const collectionName = entry.collection;
---

<p>This page is from the {collectionName} collection.</p>

The entry object contains information about the current page, including its collection name. This is available to components that are used to render content from collections.

It's important to note that this approach works when you're using Astro's Content Collections API and rendering your content through dynamic routes or content collection queries.

For more details on the properties available in the entry object, you can refer to the Astro Content Collections API Reference.

If you need to access this information in a child component, you would need to pass it as a prop from the parent component where the entry is available.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: