Skip to content

Instantly share code, notes, and snippets.

@tentacode
Created October 15, 2012 12:23
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save tentacode/3892186 to your computer and use it in GitHub Desktop.
Save tentacode/3892186 to your computer and use it in GitHub Desktop.
Twig recursive macro
{% macro recursiveCategory(category) %}
<li>
<h4><a href="{{ path(category.route, category.routeParams) }}">{{ category }}</a></h4>
{% if category.children|length %}
<ul>
{% for child in category.children %}
{{ _self.recursiveCategory(child) }}
{% endfor %}
</ul>
{% endif %}
</li>
{% endmacro %}
{% if categories %}
<div id="categories">
<ul>
{% for category in categories %}
{{ _self.recursiveCategory(category) }}
{% endfor %}
</ul>
</div>
{% endif %}
@AlexanderMatveev
Copy link

@ureimers your example gives exception:

Impossible to invoke a method ("recursiveCategory") on a string variable ("...html.twig").

@Smanst3r
Copy link

Hi!
How to be with categories duplicates.
I mean if category has been rendered as a child, but it also will be rendered as a root category? So on page will be the two same categories rendered as a child category and as a root category.
Kind regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment