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

Navigation Problem

Started by Muut Archive 11 years ago · 11 replies · 536 views
11 years ago

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

11 years ago

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?

11 years ago

Also please, please, please paste big blocks of code between triple back ticks (```)

11 years ago

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.

11 years ago

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.

11 years ago

maybe something to run this script always relative to the root? A loop or something? I'm really a newbie to grav..

11 years ago

or can i access the page template name with a function? like {{ page.template }} ?

11 years ago

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.

11 years ago

Finally I can figured it out.

Here is the final code:

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

11 years ago

I am glad it's working, but still am not sure why:

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

11 years ago

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 <ul>s as you can see above.

11 years ago

Ok, i'll leave it be! There really is no wrong answer anyway :)

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1338 9 years ago
Archive · by Muut Archive, 9 years ago
2 927 9 years ago
Archive · by Muut Archive, 9 years ago
2 4056 9 years ago
Archive · by Muut Archive, 9 years ago
1 2941 9 years ago
Archive · by Muut Archive, 9 years ago
3 1112 9 years ago