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)

Most upvoted comments

Here is the patch you need to apply to make it work:

diff --git a/src/Twig/Node/TreeNode.php b/src/Twig/Node/TreeNode.php
index 4f5fb0d..c637bdd 100644
--- a/src/Twig/Node/TreeNode.php
+++ b/src/Twig/Node/TreeNode.php
@@ -131,7 +131,7 @@ class TreeNode extends Node
         $compiler
             ->outdent()
             ->write("};\n")
-            ->write("\$tree_")
+            ->write("yield from \$tree_")
             ->raw($this->getAttribute('as'))
             ->raw("(")
             ->subcompile($this->getNode('seq'))

You will also need to remove the usage of the twig_ensure_traversable function 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.

Thanks. I think I’ve installed it correctly, and the tree works, but there’s a deprecation error. When I go to the debugger to see what it is, I get

Cannot assign Symfony\Component\VarDumper\Cloner\Data to property Twig\Runtime\Loop::$seq of type Iterator"

Th line I changed, but it could also be related to the yield statements above.

->write("\$context['_seq'] = is_iterable(\$data) ? \$data : [];\n")
An exception has been thrown during the rendering of a template ("Cannot assign Symfony\Component\VarDumper\Cloner\Data to property Twig\Runtime\Loop::$seq of type Iterator").
Twig\Error\
RuntimeError
Show exception properties
in vendor/symfony/web-profiler-bundle/Resources/views/Collector/logger.html.twig (line 516)
in [var/cache/dev/twig/4c/4c1110c5dbe44e789a03d9a1bf20ad14.php ](file:///home/tac/g/tacman/twig-tree-tag-demo/var/cache/dev/twig/4c/4c1110c5dbe44e789a03d9a1bf20ad14.php#L103)-> yieldBlock (line 103)

Good catch, fixed by #4141