#Passing data to layouts
3 messages · Page 1 of 1 (latest)
Hi @slender sleet. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
You can pass page-specific data to a layout in Astro by using component attributes. Here's a step-by-step guide on how to do it:
- Pass the page title to your layout component from
index.astrousing a component attribute. For example:
---import BaseLayout from '../layouts/BaseLayout.astro';
const pageTitle = "Home Page";
---
<BaseLayout pageTitle={pageTitle}>
<h2>My awesome blog subtitle</h2>
</BaseLayout>
In this example, pageTitle is a variable defined in the frontmatter script of index.astro and its value is passed to the BaseLayout component as a prop.
- Change the script of your
BaseLayout.astrolayout component to receive a page title viaAstro.propsinstead of defining it as a constant. For example:
---import Header from '../components/Header.astro';
import Footer from '../components/Footer.astro';
import '../styles/global.css';
const { pageTitle } = Astro.props;
---
In this example, pageTitle is a prop received from the parent component (index.astro).
- Check your browser preview to verify that your page title has not changed. It has the same value, but is now being rendered dynamically. And now, each individual page can specify its own title to the layout.