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.

Archive

Acces Modular Parent from Modular

Started by Muut Archive 11 years ago · 3 replies · 1023 views
11 years ago

This is a total Newbie question, but I am not sure where to look for the answer. I am looking at the Modular example, and instead of using the buttons information in the showcase module to replace the header nav in the modular.html.twig, I want to iterate through the modular sections and show them as buttons inside of the showcase.html.twig.

So how do I do this? How do I get access to the "parent" modular from within the modular page

Instead of this inside showcase.html.twig

TWIG
   {% for button in page.header.buttons %}
        <a class="button{% if button.primary %} primary{% endif %}" href="{{ button.url }}">{{ button.text }}</a>
    {% endfor %}

I want to do something like this:

TWIG
    {% for module in page.activeParent.collection() %}

            {% set current_module = (module.active or module.activeChild) ? 'active' : '' %}
            <a class="button"href="#{{ _self.pageLinkName(module.menu) }}">{{ module.menu }}</a>

    {% endfor %}

Also is this TWIG syntax or PHP or Grave? What documentation should Iook at for this to understand the syntax.

Thanks

11 years ago

ok I am almost there. I have the parent part figured out, the button shows the proper name but the link is empty. Why is the {{ _self.pageLinkName(module.menu) }} not working

TWIG

    {% for module in page.parent.collection() %}

            {% set current_module = (module.active or module.activeChild) ? 'active' : '' %}
            <a class="button"href="#{{ _self.pageLinkName(module.menu) }}">{{ module.menu }}</a>

    {% endfor %}

---
11 years ago

Problem solved. I did not realize that the pageLinkName was a macro that was available in the parent. I copied it to the partial and all is good now:

updated showcase:

TWIG

{% macro pageLinkName(text) %},{{ text|lower|replace({' ':'_'}) }},{% endmacro %}

{% set showcase_image = page.media.images|first.grayscale().contrast(20).brightness(-125).colo rize(-35,81,122) %}
{% if showcase_image %}
    <div class="modular-row showcase flush-top" style="background-image: url({{ showcase_image.url }});">
{% else %}
<div class="modular-row showcase">
{% endif %}
    {{ content }}

    {% for module in page.parent.collection() %}

            {% set current_module = (module.active or module.activeChild) ? 'active' : '' %}
            {% if module.menu != "Top" %}
            <a class="button"href="#{{ _self.pageLinkName(module.menu) }}">{{ module.menu }}</a>
            {% endif %}
    {% endfor %}
</div> 
---
11 years ago

Glad I could be of so much help! :) Seriously glad you sorted it out, your example will prove helpful to others i'm sure.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1282 9 years ago
Archive · by Muut Archive, 9 years ago
2 889 9 years ago
Archive · by Muut Archive, 9 years ago
2 4019 9 years ago
Archive · by Muut Archive, 9 years ago
1 2894 9 years ago
Archive · by Muut Archive, 9 years ago
3 1078 9 years ago