Combine layouts to get the best of both worlds
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
Now that you have added a layout to each blog post, it’s time to make your posts look like the rest of the pages on your website!
Mach dich bereit, …
- Nest your blog post layout inside your main page layout
Nest your two layouts
Section titled Nest your two layoutsYou already have a BaseLayout.astro
for defining the overall layout of your pages.
MarkdownPostLayout.astro
gives you some additional templating for common blog post properties such as title
and date
, but your blog posts don’t look like the other pages on your site. You can match the look of your blog posts to the rest of your site by nesting layouts.
-
In
src/layouts/MarkdownPostLayout.astro
, importBaseLayout.astro
and use it to wrap the entire template content. Don’t forget to pass thepageTitle
prop: -
Check your browser preview at
http://localhost:4321/posts/post-1
. Now you should see content rendered by:- Your main page layout, including your styles, navigation links, and social footer.
- Your blog post layout, including frontmatter properties like the description, date, title, and image.
- Your individual blog post Markdown content, including just the text written in this post.
-
Notice that your page title is now displayed twice, once by each layout.
Remove the line that displays your page title from
MarkdownPostLayout.astro
: -
Check your browser preview again at
http://localhost:4321/posts/post-1
and verify that this line is no longer displayed and that your title is only displayed once. Make any other adjustments necessary to ensure that you do not have any duplicated content.
Make sure that:
-
Each blog post shows the same page template, and no content is missing. (If one of your blog posts is missing content, check its frontmatter properties.)
-
No content is duplicated on a page. (If something is being rendered twice, then be sure to remove it from
MarkdownPostLayout.astro
.)
If you’d like to customize your page template, you can.
Test your knowledge
Section titled Test your knowledge-
This allows you to nest one layout inside another and take advantage of working with modular pieces.
-
Multiple layouts are particularly useful for projects that contain Markdown pages, like a…
-
Which of these provides templating for all your pages?
Checklist
Section titled ChecklistResources
Section titled ResourcesCreate and pass data to a custom blog layout Nächste Seite
Check in: Unit 5 - Astro API