Skip to content

Instantly share code, notes, and snippets.

@isthatcentered
Last active June 21, 2024 17:50

Revisions

  1. isthatcentered revised this gist Oct 16, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions recursive_list.twig
    Original file line number Diff line number Diff line change
    @@ -22,8 +22,8 @@
    {# Nav Being the variable passed to the template #}
    {% if nav %}
    {# importing the macro created earlier #}
    {% import _self as recursive %}
    {% import _self as tree %}

    {# using macro {nameOfImport}.{nameOfMethod}(variable) #}
    {{ recursive.list(nav.children) }}
    {{ tree.list(nav.children) }}
    {% endif %}
  2. isthatcentered created this gist Oct 16, 2016.
    29 changes: 29 additions & 0 deletions recursive_list.twig
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    {% macro list(items, class) %}
    <ul class="{{class}}">

    {# Iterating over each direct items #}
    {% for item in items %}
    <li>
    <a href="">{{item.name}}</a>

    {# If an item has children #}
    {% if item.children %}
    {# Re-import the macro #}
    {% import _self as recursive %}
    {# And use it to iterate over item's children #}
    {{ recursive.list(item.children) }}
    {% endif %}
    </li>
    {% endfor %}
    </ul>
    {% endmacro %}


    {# Nav Being the variable passed to the template #}
    {% if nav %}
    {# importing the macro created earlier #}
    {% import _self as recursive %}

    {# using macro {nameOfImport}.{nameOfMethod}(variable) #}
    {{ recursive.list(nav.children) }}
    {% endif %}