Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

TWIG: Display/Access module from modular in a non-modular page

Solved by Marcus Marcus View solution

Started by Marcus Marcus 6 years ago · 9 replies · 688 views
6 years ago

Dear community,

in Twig, how is it possible to display a module (footer) from a modular page (home) inside of all parent pages (e. g. legal)


pages

01.home (modular)
--- 01._hero
--- 02._main
--- 03._footer -> this one …

02.legal -> … should also be visible here


Thanks a lot!
Marcus

last edited 10/31/20 by Marcus Marcus
6 years ago

In your template include file
[code]
{% include 'partials/footer.html.twig' %}
[/code]
or is there something special in your theme ?

6 years ago

Hey 👋🏼 First of all, thank you. Simply including the file would include also the

TWIG
{{ page.content }}

… defined in footer twig, but grabbing the page content of the current page (02.legal)

That’s my problem.

6 years ago

Is it a block to be included on specific pages or all site ?

6 years ago

Sorry, my fault. I wasn’t detailed enough.

The Module Footer (which is part of modular Home) should be visible all site.

Pages structure:

01.home (modular)
— 01._hero
— 02._main
— 03._footer -> this one …

02.legal -> … should also be visible here
03.another page -> … should also be visible here
04. … -> … should also be visible here


Footer Module (footer.html.twig):

TWIG
<footer id="{{ page.menu|hyphenize }}" class="footer theme-dark">
    <article class="grid">
        {{ page.content|markdown }}
        {% include 'partials/langswitcher.html.twig' %}        
    </article>
</footer>


default.html.twig:

TWIG
{% extends 'partials/legal.html.twig' %}

{% block page_content %}
    <main>
        {{ page.content }}
    </main>
    {% include 'modular/footer.html.twig' %}   
{% endblock %}

6 years ago

If I understand, the footer is not static but dynamique through the backoffice ?

6 years ago Solution

Ok, I got it. Funny that it took me quite a lot of time to find out, that the solution already has been documented in the cookbook!:
https://learn.getgrav.org/16/cookbook/general-recipes#reuse-page-or-modular-content-on-another-page

In my Case the solution was:

TWIG
{% extends 'partials/legal.html.twig' %}

{% block page_content %}

    <main>
        {{ page.content }}
    </main>

    {% include 'modular/modular_reuse.html.twig' with {'page': page.find('/home/_footer')} %}

{% endblock %}

Thanks a lot @Kit

6 years ago

👍

there is also this method

[CODE]
{% if page.find('/modules/sidebar').content %}
{{ page.find('/modules/sidebar').content|raw }}
{% endif %}
[/CODE]

In backoffice add "Folder" and add page with same name (exemple : sidebar)
grav-page-find|480x156, 50%

6 years ago

Since the legal info will occur on all pages, I would consider moving the content to a global config file like user/config/legal.yaml and populate the template from this. You could also make a corresponding blueprint to edit the content in the admin.

With the legal content in a module of the home page, It would not be obvious where that content would be found without instructions.

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 198 2 months ago
Themes & Styling · by Ian, 2 months ago
3 94 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 456 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 48 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 128 3 months ago