-
Hi all! On a page basis we want to add (optional) ‘meta data’–like page headers, page footers, Global Nunjucks variables are not supported. So I came up with the code below. As we are planning a rather large-scale eleventy project I would like to ask you: Is this the way to go, or would you suggest another method? Front matter for Thank you very much in advance! Content file:
Partial
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
I think support for Nunjucks globals was added in #1060 (which will ship w/ Eleventy v1.0). 🎉 |
Beta Was this translation helpful? Give feedback.
-
I'm not sure if I follow 100%, but I don't know if that will work (although admittedly I haven't tried building it locally yet). I think my main concerns would be that <header class="page-header">
My page header 2 content.
</header>
<footer class="page-footer">
My page footer content.
</footer> Although now you've made me very curious whether I guessed correctly, so I'll try building a small proof of concept locally. ---
title: Test page
---
{%- set pageHeader -%}
My page header 1 content.
{%- endset -%}
{% include "meta.njk" %}
{%- set pageHeader -%}
My page header 2 content.
{%- endset -%}
{% include "meta.njk" %}
<!-- page contents -->
{%- set pageFooter -%}
My page footer content.
{%- endset -%}
{% include "meta.njk" %} |
Beta Was this translation helpful? Give feedback.
-
Excellent! Thank you very much for you support and your estimation. Using seperate "metaHeader.njk" and "metaFooter.njk" partials is a practical solution. I had thought of using macros, shortcodes and and methods; but your comments encourage me to use the partial method. Thanks again. |
Beta Was this translation helpful? Give feedback.
I'm not sure if I follow 100%, but I don't know if that will work (although admittedly I haven't tried building it locally yet). I think my main concerns would be that
pageHeader
gets set, and then added to the page using{% include "meta.njk" %}
. Then we overwrite the localpageHeader
variable w/ page header 2 content and add another<header class="page-header">{{ pageHeader | safe }}</header>
to the DOM. So far so good, but you'll have two<header class="page-header">
s on your page. But now when you get to the footer, I assume yourpageHeader
variable from earlier is still in scope, so your footer might have the following: