#<head> is in Layout, but want a unique <title> for each page that uses the Layout

8 messages · Page 1 of 1 (latest)

flat plinth
#

Like it says above, I'm trying to get a unique title in the top of the browser window via the title tag, but I'm using a layout/template for the <head> and top body of my page.

Sorry, I'm new to coding in general, any tips?

rare nacelleBOT
#

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

#

Sure, I can help with that. In Astro, you can pass page-specific values as props to your layout component. Here's how you can do it:

  1. Pass the page title to your layout component from your page (for example, index.astro) using a component attribute. Here's an example:
---
import BaseLayout from '../layouts/BaseLayout.astro';
const pageTitle = "Home Page";
---
<BaseLayout pageTitle={pageTitle}> 
  <h2>My awesome blog subtitle</h2>
</BaseLayout>
  1. Then, in your BaseLayout.astro layout component, receive the page title via Astro.props instead of defining it as a constant. Here's how you can do it:
---
import Header from '../components/Header.astro';
import Footer from '../components/Footer.astro';
import '../styles/global.css';
const { pageTitle } = Astro.props;
---
  1. Now, each individual page can specify its own title to the layout. Check your browser preview to verify that your page title has not changed. It has the same value, but is now being rendered dynamically.

You can find more details in the Astro documentation (https://docs.astro.build/en/tutorial/4-layouts/1#pass-page-specific-values-as-props).

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

#

@flat plinth kapa.ai is still learning and improving, please let us know how it did by reacting below

flat plinth
#

Does what you say above work with <title> in the head too though? Above seems to be for an h2 title within the body.

#

@rare nacelle Does what you say above work with <title> in the head too though? Above seems to be for an h2 title within the body.

rare nacelleBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Yes, the same concept applies to the <title> tag in the <head> section of your HTML. You can pass the title as a prop to your layout component and then use it in the <title> tag. Here's how you can do it:

  1. Pass the page title to your layout component from your page (for example, index.astro) using a component attribute. Here's an example:
---
import BaseLayout from '../layouts/BaseLayout.astro';
const pageTitle = "Home Page";
---
<BaseLayout pageTitle={pageTitle}> 
  <!-- Your page content goes here -->
</BaseLayout>
  1. Then, in your BaseLayout.astro layout component, receive the page title via Astro.props and use it in the <title> tag. Here's how you can do it:
---
const { pageTitle } = Astro.props;
---
<html>
  <head>
    <title>{pageTitle}</title>
  </head>
  <body>
    <slot />
  </body>
</html>

Now, each individual page can specify its own title to the layout. The title will be used in the <title> tag in the <head> section of your HTML, which is what is displayed in the browser tab.

You can find more details in the Astro documentation (https://docs.astro.build/en/tutorial/4-layouts/1#pass-page-specific-values-as-props).

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