Deeply Nested Menu Loop with Twig using Timber [closed]

From a usability point of view, have you considered using some sort of sub-menu (maybe a widget in a sidebar), since navigating that many nested menus might be a bit difficult.

Anyways – back to your question. yes you should use a macro. Here’s an example I’ve used before to generate a 3 level deep bootstrap menu (but it can handle more).

page.php

$context['menu']['header_nav'] = new TimberMenu('Header Nav');
Timber::render('page.twig', $context);

macros/menus.twig

{% macro embedded_list(items) %}
    {% import _self as menus %}
    {% for item in items %}
        <li>
            <a href="https://wordpress.stackexchange.com/questions/261163/{{ item.link }}">{{ item.title }}</a>
            {% if item.has_child_class %}
                <ul>
                    {{ menus.embedded_list(item.get_children) }}
                </ul>
            {% endif %}
        </li>
    {% endfor %}
{% endmacro %}

page.twig

<ul>
  {% import 'macros/menus.twig' as menus %}
  {{ menus.embeddded_list(navs.header_nav.items) }}
</ul>

I got this example from this discussion. Here’s a blog post about it as well.