Building composable layouts in Astro

One of the things I like when working on a website is to be able to fragment all the layouts in different files.

Let’s say I want to create a landing page.

The last one I did was in plain HTML. Which is nice, can’t get simpler and more minimal than that.

But then I start to realize I also want a different page on the website, which looks similar, but has the same styling and structure.

Duplication starts to happen. Then I want another page, and finally I decide to change something in the header for all pages, and it’s frustrating.

Here’s when static site builders help.

In many tools, like Hugo, you use partials.

In Astro, it’s all based on components, so we import and embed components in JSX:

---
import Header from ../components/Header.astro
import Footer from ../components/Footer.astro
---
<html>
  <body>
    <Header /> 
    <h1>A page</h1>
    <Footer />
  </body>
</html>

Then we can copy/paste this structure in our components.

Notice that there’s still some HTML that we can extract, to avoid copy/pasting it.

We do this using layouts.

You create layouts in the src/layouts folder:

src/layouts/MyLayout.astro

---
import Header from ../components/Header.astro
import Footer from ../components/Footer.astro
---
<html>
  <body>
    <Header /> 
    <slot />
    <Footer />
  </body>
</html>

Notice the use of <slot />.

This is the way we can embed anything in there, from another component.

We use the layout in this way:

src/pages/index.astro

---
import MyLayout from '../layouts/MyLayout.astro'
---
<MyLayout>
  <h1>Homepage!</h1>
</MyLayout>


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

One of the things I like when working on a website is to be able to fragment all the layouts in different files.

Let’s say I want to create a landing page.

The last one I did was in plain HTML. Which is nice, can’t get simpler and more minimal than that.

But then I start to realize I also want a different page on the website, which looks similar, but has the same styling and structure.

Duplication starts to happen. Then I want another page, and finally I decide to change something in the header for all pages, and it’s frustrating.

Here’s when static site builders help.

In many tools, like Hugo, you use partials.

In Astro, it’s all based on components, so we import and embed components in JSX:

---
import Header from ../components/Header.astro
import Footer from ../components/Footer.astro
---
<html>
  <body>
    <Header /> 
    <h1>A page</h1>
    <Footer />
  </body>
</html>

Then we can copy/paste this structure in our components.

Notice that there’s still some HTML that we can extract, to avoid copy/pasting it.

We do this using layouts.

You create layouts in the src/layouts folder:

src/layouts/MyLayout.astro

---
import Header from ../components/Header.astro
import Footer from ../components/Footer.astro
---
<html>
  <body>
    <Header /> 
    <slot />
    <Footer />
  </body>
</html>

Notice the use of <slot />.

This is the way we can embed anything in there, from another component.

We use the layout in this way:

src/pages/index.astro

---
import MyLayout from '../layouts/MyLayout.astro'
---
<MyLayout>
  <h1>Homepage!</h1>
</MyLayout>


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-11-17T05:00:00+00:00) Building composable layouts in Astro. Retrieved from https://www.scien.cx/2021/11/17/building-composable-layouts-in-astro/

MLA
" » Building composable layouts in Astro." flaviocopes.com | Sciencx - Wednesday November 17, 2021, https://www.scien.cx/2021/11/17/building-composable-layouts-in-astro/
HARVARD
flaviocopes.com | Sciencx Wednesday November 17, 2021 » Building composable layouts in Astro., viewed ,<https://www.scien.cx/2021/11/17/building-composable-layouts-in-astro/>
VANCOUVER
flaviocopes.com | Sciencx - » Building composable layouts in Astro. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/17/building-composable-layouts-in-astro/
CHICAGO
" » Building composable layouts in Astro." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/11/17/building-composable-layouts-in-astro/
IEEE
" » Building composable layouts in Astro." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/11/17/building-composable-layouts-in-astro/. [Accessed: ]
rf:citation
» Building composable layouts in Astro | flaviocopes.com | Sciencx | https://www.scien.cx/2021/11/17/building-composable-layouts-in-astro/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.