I have a set of pages that use a specific template “example.html.twig”. All the pages that use this template have a common section after their main content, but ONLY if a specific frontmatter variable is NOT set. So I have this bit of code after the main content section in my base.html.twig:
{% block extra %}
{% if page.header.status != "whatever" %}
{% include 'partials/example.html.twig' with {'page': page.find('/extra', true)} %}
{% endif %}
{% endblock %}
This basically works, but only the heading is taken from the /extra page – the text is the same as in the main section of that page. So I’m thinking there might be a conflict between the page variables in use? But which would I change, and how?
Really grateful for any ideas!
Hi. What do you mean by :
Can you copy the content of that /extra page, and show us what you want to extract ?
Also, an excerpt of the content of partials/example.html.twig
could help.
Basically, all what you do is loading the /extra page by using include 'partials/example.html.twig'
, but with a variable specification, that will be processed by the included page itself.
Then, you use with {'page': page.find('/extra', true)}
. It seems ok, except the , true
part, wich I’ve never seen before. Could you try without it ?
Hello Romarain,
thank you so much for your reply! It helped me figure out what was wrong, and it was a simple thing really.
First of all, I copied the with {'page': page.find('/extra', true)}
bit from somewhere in the Grav docs I think. I have no idea what that true
is supposed to do, and omitting it or substituting false
doesn’t change a thing. Possibly this was a useful parameter in an earlier version that has lost its meaning?
And my error was in the extra.html.twig
file! It looked like this:
<section class="section extra clearfix">
<div class="wrapper">
<h2>{{ page.header.title }}</h2>
{{ content }}
</div>
</section>
And when I put {{ page.content }}
there instead of just {{ content }}
, it all worked perfectly as intended.
1 Like
Well done ! I’m happy to have helped.
When using a variable needing to access a page frontmatter in a template, we always should precise what’s the object. There’s two objects very usefull in Grav : page()
and pages()
, wich can be accessible in the python-style chaining : page.stuff.substuff
… etc.
The page() object automatically build some things, like “header”, wich is the frontmatter and can be accessed with page.header
. And from here, we can point to a yaml field, like page.header.title
.