{# This file might seem a little complex, because of the high density of tags.
   It uses Twig macros and ternary selectors. Read up on them, if required:
   macros: http://twig.sensiolabs.org/doc/templates.html#macros
   ternary operators: http://twig.sensiolabs.org/doc/templates.html#other-operators
#}

{# Make sure the setting for the submenus is defined. #}
{% if withsubmenus is not defined %}
    {% set withsubmenus = true %}
{% endif %}

{# The 'recursive' macro, for inserting one menu item. If it has a submenu, it
   invokes itself to insert the items of the submenus. #}
{% macro display_menu_item(item, loop, extraclass, withsubmenus) %}
    {% from _self import display_menu_item %}
    {% spaceless %}
    <p class="level-item">
        <a href="{{ item.link }}" title='{{ item.title|default('')|escape }}' class='{{ item.class|default('') }}'>
            {{- item.label|default('-') -}}
        </a>
    </p>
    {% endspaceless %}
{% endmacro %}

{# Make the macro available for use #}
{% from _self import display_menu_item %}

{# The main menu loop: Iterates over the items, calling `display_menu_item` #}
{% for item in menu %}
    {% if item.label is defined %}
        {{ display_menu_item(item, loop, '', withsubmenus) }}
    {% endif %}
{% endfor %}