Twig: Twig Extension no longer rendering output
Did something change in user-defined twig extensions? This is in the category of “I swear this used to work!” My extension is at https://github.com/tacman/twig-tree-tag.
It’s a slick way of making recursive calls to tree elements, like menus
{% set food = [
{ name: 'fruits',
children: [{name: 'apple'},{name:'banana'}] },
{ name: 'veggies',
children: [
{name: 'peas'},
{name: 'carrots'}
]
}
] %}
<h1>Food Tree</h1>
{% tree item in food %}
{% if treeloop.first %}<ul>{% endif %}
<li>
{{ item.name }}
{% subtree item.children|default([]) %}
</li>
{% if treeloop.last %}</ul>{% endif %}
{% endtree %}
{% endblock %}
It used to display the nested HTML, and now nothing. It’s registered okay, but no content either. I’ve created a repo showing the bug
git clone git@github.com:tacman/twig-tree-tag-demo && cd twig-tree-tag-demo
composer install
symfony server:start -d
symfony open:local
It’s a trival app – install the library, register the extesion in services.yaml, and create a controller / template.
Did something change in twig regarding output? I vaguely remember something about twig and using the output buffer in a discussion.
It’s a cool tag, it really simplifies rendering trees by skipping creating a macro to make recursive calls. I didn’t write it, but I did upgrade it a few years ago to Twig 3, and I’m certain at some point the twig 3 version worked.
Thanks.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 20 (12 by maintainers)
Here is the patch you need to apply to make it work:
You will also need to remove the usage of the
twig_ensure_traversablefunction as it’s an internal function that has been removed.The new yield implementation BC layer has many drawbacks that were not anticipated. I need to come back to the drawing board to see how we can mitigate these problems.
Good catch, fixed by #4141