Muut
September 18, 2015, 9:12am
1
I have a really complex navigation with:
main pages > sub pages > sub pages with moduler content
My navigation just works fine with this code on the main pages:
{% for page in pages.children %}
{% if page.active %}
{% if page.children %}
{% for child in page.children %}
{% if child.visible %}
{{ child.menu }}
{% if child.children %}
{% for lilchild in child.children %}
{% if lilchild.visible %}
{% set current_lil = (lilchild.active or lil.activeChild) ? ‘active’ : ‘’ %}
{{ lilchild.menu }}
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
{% endfor %}
But when I want to render the navigation with this code, nothing renders. I know what the problem is, just want to render the same menu on the sub pages with modular content corresponding to there main page.
Thanks
Muut
September 18, 2015, 12:39pm
2
I was following ok up to this line:
@valen
I know what the problem is, just want to render the same menu on the sub pages with modular content corresponding to there main page.
What does this mean? Menu works on all pages, just not on the page with teh modular content?
Muut
September 18, 2015, 12:40pm
3
Also please, please, please paste big blocks of code between triple back ticks (```)
Muut
September 18, 2015, 1:45pm
4
Sorry for the code…
Sadly, I can’t provide you a demo because it’s under a .dmz .
The menu works only on the main pages.
Muut
September 18, 2015, 1:49pm
5
it ‘could’ be that your looping over the pages
and using page
which is already a variable assigned to the Twig environment. Try changing this page
variable to p
or something more unique.
Muut
September 18, 2015, 1:53pm
6
maybe something to run this script always relative to the root? A loop or something? I’m really a newbie to grav…
Muut
September 18, 2015, 1:57pm
7
or can i access the page template name with a function? like {{ page.template }} ?
Muut
September 18, 2015, 2:30pm
8
pages
should always be available and always contains ‘all’ the pages.
{{ page.template }}
should return the template name, but why do you need it?
Maybe just send me a zip file of your site to andy at getgrav dot org, and i’ll try to get a better understanding of your issue.
Muut
September 18, 2015, 2:43pm
9
Finally I can figured it out.
Here is the final code:
{% if page.template == "main" %}
{% set main = page %}
{% elseif page.template == "sub" %}
{% set main = page.parent %}
{% elseif page.template == "page" %}
{% set main = page.parent.parent %}
{% endif %}
{% if main.children %}
{% for child in main.children %}
{% if child.visible %}
<ul class="app--side_menu-block">
<li class="head"><a href="{{ child.url }}">{{ child.menu }}</a></li>
{% if child.children %}
{% for lilchild in child.children %}
{% if lilchild.visible %}
{% set current_lil = (lilchild.active or lil.activeChild) ? 'active' : '' %}
<li class="item {{ current_lil }}"><a href="{{ lilchild.url }}">{{ lilchild.menu }}</a></li>
{% endif %}
{% endfor %}
{% endif %}
</ul>
{% endif %}
{% endfor %}
{% endif %}
Maybe there is a better way to do it, but it just works fine.
Muut
September 18, 2015, 2:52pm
10
I am glad it’s working, but still am not sure why:
{% for child in pages.children %}
Wouldn’t work for all pages? That’s what I do on my test sites and have never had a problem.
Muut
September 18, 2015, 3:07pm
11
Nope… Because I’m rendering the “main” pages in a different navigation. I just needed to list all the “sub” pages with there “pages” in
Muut
September 18, 2015, 3:22pm
12
Ok, i’ll leave it be! There really is no wrong answer anyway