Macro changes thanks to twig 2
Created 6 years ago by edster

So i ran into this issue today where my macros for navigation all broke.

So this is a PSA to put {% import _self as self %} into your macros and change any _self references in your code to self.

edster  —  6 years ago Best Answer

Example:

{% macro tree(items, tree) %}

    {% import _self as self %}

    {% for item in items %}
        <li data-id="{{ item.id }}">
            <div class="card">

                {% if tree.options.get('sortable', true) %}
                    {{ icon('bars', 'handle btn btn-xs') }}
                {% endif %}

                {% for segment in item.segments %}
                    {% if segment.value %}
                        <span class="{{ segment.class }}" {{ html_attributes(segment.attributes) }}>
                        {{ segment.value|raw }}
                    </span>
                    {% endif %}
                {% endfor %}

                <div class="buttons">
                    {{ buttons(item.buttons) }}
                </div>

            </div>

            <ul>
                {{ self.tree(tree.items.children(item), tree) }}
            </ul>
        </li>
    {% endfor %}
{% endmacro %}