{% macro loop(page) %}
    {% for p in page.children.visible %}
        <li class="{% if p.children.visible.count > 0 %}dropdown{% endif %}">
            {% if p.children.visible.count > 0 %}
            <a class="dropdown-toggle" data-toggle="dropdown" href="{{ p.url }}">
                {% if p.header.icon %}<i class="fa fa-{{ p.header.icon }}"></i>{% endif %}
                {{ p.menu }}
                <i class="fa fa-caret-down"></i>
            </a>
            {% else %}
                <a href="{{ p.url }}">
                    {% if p.header.icon %}<i class="fa fa-{{ p.header.icon }}"></i>{% endif %}
                    {{ p.menu }}
                </a>
            {% endif %}
            {% if p.children.visible.count > 0 %}
                <ul class="dropdown-menu">
                    {{ _self.loop(p) }}
                </ul>
            {% endif %}
        </li>
    {% endfor %}
{% endmacro %}

<ul class="nav" id="menu-primary">
    {% if theme_config.dropdown.enabled %}
        {{ _self.loop(pages) }}
    {% else %}
        {% for page in pages.children.visible %}
            {% set current_page = (page.active or page.activeChild) ? 'current' : '' %}
            <li class="{{ current_page }}">
                <a href="{{ page.url }}">
                    {{ page.menu }}
                </a>
            </li>
        {% endfor %}
    {% endif %}
    {% for mitem in site.menu %}
        <li>
            <a href="{{ mitem.url }}">
                {{ mitem.text }}<i class="fa fa-{{ mitem.icon }}"></i>
            </a>
        </li>
    {% endfor %}
    <li class="dropdown">
        <a class="dropdown-toggle" data-toggle="dropdown">
            <i class="fa fa-language"></i>
            <i class="fa fa-caret-down"></i>
        </a>
        <ul class="dropdown-menu">
            {% include 'partials/langswitcher.html.twig' %}
        </ul>
    </li>
    {% if config.plugins.login.enabled and grav.user.username %}
        <li>{% include 'partials/login-status.html.twig' %}</li>
    {% endif %}
</ul>
