eleventy: What is the best way to pass a block from a layout to a parent layout?
I’m using Nunjucks layouts, and the layout attribute in Front Matter for layout chaining, so that a page.njk layout uses a base.njk layout.
As I read it, the Addendum about existing Templating features states that “Eleventy’s layout system exists a layer above Nunjucks’ Template Inheritance exposed with {% extends %}“.
So I thought {% extends %} was implicit when using layout chaining, but a {% block %} defined in my page.njk layout never arrives into base.njk.
I tried to add an explicit {% extends %} like I saw in Phil Hawksworth’s code, but I get the base.njk included twice, kind of recursively
If I remove the layout attribute from the Front Matter of the page.njk layout, its content doesn’t arrive into base.njk anymore.
I must be missing something obvious, so how is it supposed to work?
Thanks
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 8
- Comments: 16 (5 by maintainers)
Commits related to this issue
- Replace Eleventy layout chaining with Nunjucks extends See https://github.com/11ty/eleventy/issues/853#issuecomment-574371277 — committed to nhoizey/precious-prana.com by nhoizey 4 years ago
Default support for blocks would be super nice.
In the mean time, I’ve set up these tags to recreate similar functionality, maybe it’s of help to someone else.
Define a
layoutblockblock with namefoolike this in the layout template:Set the contents of
layoutblockwith namefooin a document like this:Oh, looks like @zachleat already answered this is “by design”: https://github.com/11ty/eleventy/issues/834#issuecomment-569474008
That’s not why I understood when reading the Addendum about existing Templating features, maybe we could rephrase it.
@rikschennink @gluecksmensch as we showed before in the thread, you can use Nunjucks’
{% extend … %}directly, without additional shortcode, but you can’t use Eleventy’slayoutFront Matter with it.I do this here for example: https://github.com/nhoizey/precious-prana.com/blob/master/src/_includes/layouts/evenement.njk#L2
And here’s the
base.njklayout definig blocks: https://github.com/nhoizey/precious-prana.com/blob/master/src/_includes/layouts/base.njk(Edit: sorry, @gluecksmensch, it didn’t work for me without the
.page. Not sure why…) Thanks so much @rikschennink This works for me also 👍 Though for me it was also outputtingundefinedwhere I was using the blocks (or had no content in the block) so I modified it slightly. Hopefully it helps someone:Usage reminder (thanks again, @rikschennink): Define a
layoutblockblock with namefoolike this in the layout template:Set the contents of
layoutblockwith namefooin a document like this:This is not well documented. Some starter projects examples use the
{% extends ... %}approach and some use theYMLfrontmatter approach. Specially when it comes to choosing between mardown index vs template-engine index. It’s realy weird that you can have frontmatter and Pug (e.g) in the same file, but Pugblocks won't be assimilated as they should. This is basically changing the engines’ default functionality and introducing unexpected behaviour without mentioning it.@gluecksmensch good one.
Previous iteration I stored data in
const layoutblocks = []but that didn’t work ( obviously 😄 ) forgot to remove.I guess with my templates
pageis defined, didn’t realise this wasn’t always the case, nice improvement 👍@rikschennink Thanks for the suggestion!
this.page.layoutblockdoesn’t work becausethis.pageisundefined. And the variableconst layoutblocks = []is obsolete as it’s not used. But this worked for me:If I recall correctly, in Eleventy, Nunjucks happily passes content through to the specified layout without using
extendsif you are just populating thecontent. When you have multiple blocks that you wish to pass through to a layout, then you need to useextends.At least, this seems to work for me™
I’m struggling to find the documentation I thought had lead me to that conclusion.