[SOLVED] Display specific navigation level

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

{% 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

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?

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

...
...	
	{% 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 Like

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

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 Like