htmltwigmarkdowngrav

How to get page title from footer when it’s included as a partial?


I have a blog page and trying to add a footer as a partial , but the title of the blog page from footer.md is not correctly displayed

I’ve this code to the base.html.twig : {% include 'partials/footer.html.twig' %}

pages/footer/default.md

---
title: Footer
routable: false
visible: false
process:
    markdown: true
    twig: true
twig_first: true
---
<h1>{{ page.title }}</h1>

partials/footer.hml.twig:

<section id="footer" class="section bg-gray">
    <section class="container {{ grid_size }}">
      {% set footer_content = pages.find('/footer').content %}
      {% if footer_content %}
        {{ footer_content }}
      {% else %}
        <p><a href="http://getgrav.org">Grav</a> was <i class="fa fa-code"></i> with <i class="fa fa-heart-o pulse "></i> by <a href="https://trilby.media">Trilby Media</a>.</p>
      {% endif %}
    </section>
</section>

Whatever the title of blog pages I always have as a title in the footer the title of the page footer, i.e. Footer

is it possible to have the the title of blog pages ?


Solution

  • Pages are parsed upfront, not at the moment when called in Twig using {% set content = pages.find('/footer') %}. During parsing, the context is the page itself, so {{ page.title }} inside its Markdown will always refer to the current page object. Hence the value <h1>Footer</h1> in your example.

    However the Markdown of a page object can be parsed inside Twig using the Grav function evaluate_twig. See Grav's docs on Twig Filters & Functions

    For example: