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

Display a an area if there is a page available

Started by Muut Archive 11 years ago · 3 replies · 239 views
11 years ago
TWIG

<!-- Navigation 4 -->
            <section>

                {% for p in pages.children %}

                    {% if (p.active or p.activeChild) %}
                        {% for c in p.children %}
                            {% if (c.active or c.activeChild) %}

                                {% for cc in c.children %}
                                    {% if (cc.active or cc.activeChild) %}

                                        {% for ccc in cc.children %}
                                            {% if (ccc.active) %}
                                                <a class="active" href="{{ ccc.url }}">{{ ccc.title }}</a>
                                            {% else %}
                                                <a href="{{ ccc.url }}">{{ ccc.title }}</a>
                                            {% endif %}
                                        {% endfor %} 

                                    {% endif %}
                                 {% endfor %}

                            {% endif %}
                        {% endfor %}
                    {% endif %}

                {% endfor %}

            </section>

how can i display only the section if there is a childpage available?

thanks!

11 years ago

Hi! I'm not completely sure what you want to accomplish, but you can use children.count to see how many children there are. So you could do that loop before the section starts to detect if there are children on the third level like your section seems to display and wrap the section in an if test like so:

TWIG
<!-- Navigation 4 -->
{% set child_available = false %}
{% for p in pages.children %}
    {% if (p.active or p.activeChild) %}
        {% for c in p.children %}
            {% if (c.active or c.activeChild) %}
                {% for cc in c.children %}
                    {% if (cc.active or cc.activeChild) %}
                        {% if (cc.children > 0) %}
                            {# OK, we found a child on the third level, set the variable to true! #}
                            {{ set child_available = true }}
                        {% endif %}
                    {% endif %}
                 {% endfor %}
            {% endif %}
        {% endfor %}
    {% endif %} 
{% endfor %}

{% if (child_available) %}
<section>

    {% for p in pages.children %}

        {% if (p.active or p.activeChild) %}
            {% for c in p.children %}
                {% if (c.active or c.activeChild) %}

                    {% for cc in c.children %}
                        {% if (cc.active or cc.activeChild) %}

                            {% for ccc in cc.children %}
                                {% if (ccc.active) %}
                                    <a class="active" href="{{ ccc.url }}">{{ ccc.title }}</a>
                                {% else %}
                                    <a href="{{ ccc.url }}">{{ ccc.title }}</a>
                                {% endif %}
                            {% endfor %}

                        {% endif %}
                     {% endfor %}

                {% endif %}
            {% endfor %}
        {% endif %}

    {% endfor %}

</section>
{% endif %}

Does that answer your question?

11 years ago
TWIG
<!-- Navigation 4 -->
{% set child_available = false %}
{% for p in pages.children %}
    {% if (p.active or p.activeChild) %}
        {% for c in p.children %}
            {% if (c.active or c.activeChild) %}
                {% for cc in c.children %}
                    {% if (cc.active or cc.activeChild) %}
                        {% if (cc.children.count > 0) %}
                            {% set child_available = true %}
                        {% endif %}
                    {% endif %}
                 {% endfor %}
            {% endif %}
        {% endfor %}
    {% endif %}
{% endfor %}

your code had some mistakes, so i've overwritten some stuff:


{% if (cc.children.count > 0) %}
{% set child_available = true %}
{% endif %}


Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1323 9 years ago
Archive · by Muut Archive, 9 years ago
2 919 9 years ago
Archive · by Muut Archive, 9 years ago
2 4048 9 years ago
Archive · by Muut Archive, 9 years ago
1 2923 9 years ago
Archive · by Muut Archive, 9 years ago
3 1106 9 years ago