Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the best way to pass a block from a layout to a parent layout? #853

Open
nhoizey opened this issue Jan 13, 2020 · 4 comments
Open

What is the best way to pass a block from a layout to a parent layout? #853

nhoizey opened this issue Jan 13, 2020 · 4 comments
Labels

Comments

@nhoizey
Copy link
Contributor

@nhoizey nhoizey commented Jan 13, 2020

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

@nhoizey nhoizey added the education label Jan 13, 2020
@philhawksworth

This comment has been minimized.

Copy link

@philhawksworth philhawksworth commented Jan 13, 2020

If I recall correctly, in Eleventy, Nunjucks happily passes content through to the specified layout without using extends if you are just populating the content. When you have multiple blocks that you wish to pass through to a layout, then you need to use extends.

At least, this seems to work for me™

I'm struggling to find the documentation I thought had lead me to that conclusion.

@nhoizey

This comment has been minimized.

Copy link
Contributor Author

@nhoizey nhoizey commented Jan 14, 2020

@philhawksworth what I see in your aforementioned post.njk layout is that you have an explicit {% extends … %}, but no Front Matter and no content outside {% block … %}s.

So it looks like it's either one or the other:

  • Eleventy layout chaining with Front Matter layout property
    OR
  • an {% extends … %} and a series of {% block … %}s

The later seems to be the best (or even the only way) to "pass" an HTML fragment (a "block") to the parent (extended) layout.

@nhoizey

This comment has been minimized.

Copy link
Contributor Author

@nhoizey nhoizey commented Jan 14, 2020

Oh, looks like @zachleat already answered this is "by design":
#834 (comment)

That's not why I understood when reading the Addendum about existing Templating features, maybe we could rephrase it.

nhoizey added a commit to nhoizey/precious-prana.com that referenced this issue Jan 14, 2020
@philhawksworth

This comment has been minimized.

Copy link

@philhawksworth philhawksworth commented Jan 15, 2020

Aha. Yeah, there it is. Nice one @nhoizey!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.