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

How to limit nav recursion

Started by Muut Archive 10 years ago · 4 replies · 642 views
10 years ago

How can I limit the amount of times a Navigation loop recurses over and finds children? I only want it to do it twice but its doing it infinity times... using Antimatter basic navigation... thanks

10 years ago

you can use Twig Slice or loop.index to limit the output of your function, example:

TWIG
{% for child in page.children|slice(0,1) %}
{# Will output the two first children #}
{% endfor %}

Or alternatively

TWIG
{% for child in page.children if loop.index < 3 %}
{# Will output the two first children (0 not indexed) #}
{% endfor %}
10 years ago

Hey I can't work it out - if you have a minute can you tell me where I put the slice to make it work in this?

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>
10 years ago

I think you might have to tweak it a bit.
There is maybe a better solution.

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 and loop.index < 3%}
            <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'm not sure about what happen to the increment of loop.index inside a if, so here is a solution that should work with an incremented variable. Not very elegant though. As I said, there might be some better solutions.

TWIG
{% macro loop(page) %}
    {% for p in page.children.visible %}
{% set myvar = 1 %}
        {% set current_page = (p.active or p.activeChild) ? 'active' : '' %}
        {% if p.children.visible.count > 0 and myvar < 3 %}
             {% set myvar = myvar + 1 %}
            <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>
10 years ago

I can't edit my post, but thinking about my first version again, I'm sure it won't work.
The second one should do the trick

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1357 9 years ago
Archive · by Muut Archive, 9 years ago
2 936 9 years ago
Archive · by Muut Archive, 9 years ago
2 4066 9 years ago
Archive · by Muut Archive, 9 years ago
1 2955 9 years ago
Archive · by Muut Archive, 9 years ago
3 1121 9 years ago