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.

Support

[SOLVED] Display specific navigation level

Solved by Michal Koza View solution

Started by Christian 8 years ago · 3 replies · 1471 views
8 years ago

Hi,
I'm looking for a way to display only a specific party of my navigation in the sidebar. I'm using the Antimatter theme. What I accomplished is, I copied the relevant parts from navigation.html.twig to the sidebar.html.twig

TWIG
{% macro loop(page) %}
{% for p in page.children.visible %}
    {% set current_page = (p.active or p.activeChild) ? 'active' : '' %}
        {% if p.children.visible.count > 0 %}
            <li class="has-children {{ current_page }}">
                <a href="{{ p.url }}">
                    {% if p.header.icon %}<i class="fa fa-{{ p.header.icon }}"></i>{% endif %}
                    {{ p.menu }}
                    <span></span>
                </a>
                <ul>
                    {{ _self.loop(p) }}
                </ul>
            </li>
        {% else %}
            <li class="{{ current_page }}">
                <a href="{{ p.url }}">
                    {% if p.header.icon %}<i class="fa fa-{{ p.header.icon }}"></i>{% endif %}
                    {{ p.menu }}
                </a>
            </li>
    {% endif %}
{% endfor %}
{% endmacro %}

<ul class="navigation">
    {{ _self.loop(pages) }}
    {% for mitem in site.menu %}
        <li>
            <a href="{{ mitem.url }}">
                {% if mitem.icon %}<i class="fa fa-{{ mitem.icon }}"></i>{% endif %}
                {{ mitem.text }}
            </a>
        </li>
    {% endfor %}
</ul>

I have a navigation with at least 3 levels

TXT
ROOT
 |-A LEVEL 1
 |      '-A LEVEL 2
 |           '-A LEVEL 3
 |-B LEVEL 1
 |     '-B LEVEL 2
 |           '-B LEVEL 3
 '-C LEVEL 1

Whenever I am located in a subbranch of LEVEL 1, I only want to display the complete structure of its subbranch. For example, if I am in A Level 2, I want to display the whole A tree. If I am in B Level 3 I want the complete B tree.

May somebody help?

last edited 01/25/18 by Christian
8 years ago Solution

Try this: {% if not (p.parent.root and current_page == "" and parent_page == "" )%}
I tested it now, it should work:

JS
...
... 
    {% set current_page = (p.active or p.activeChild) ? 'active' : '' %}
        {% if not (p.parent.root and current_page == "" and parent_page == "" )%}
            {% if p.children.visible.count > 0 %}
...
...
        {% endif %}
    {% endif %}
{% endfor %}
...

👍 1
8 years ago

Working like a charm. Thank you very much.
Could you explain the theory behind this a bit?

8 years ago

Sorry for little mistake, this code:
{% if not (p.parent.root and current_page == "" )%}
should work for you better.
parent_page == "" is useless here. I used whole row from my code based on Learn theme. Here I am using both variables: "current_page" and "parent_page".

Theory behind? When page or its child is active "current_page" is equal to "active" and is not equal to "" - see one row before. And this is what you need - follow the tree of active pages only.

You can use also this code:
{% if not p.parent.root or current_page == "active" %}
it is also working and, maybe, this is more clear.
(sorry for my english)

👍 1

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 51 8 hours ago
Support · by Anna, 2 days ago
2 58 10 hours ago
Support · by Justin Young, 11 hours ago
1 30 11 hours ago
Support · by Duc , 1 week ago
2 63 5 days ago
Support · by Colin Hume, 1 week ago
2 55 5 days ago