Closed tacman closed 18 hours ago
Twig has a new yield-based implementation replacing the implementation based on output buffering (which will make it compatible with Fiber-based code as it avoids the global output buffering that would be shared between fibers). Twig has a BC layer for non-yield-ready nodes. But maybe this BC layer fails to automatically handle your TreeNode logic.
Thanks, @stof . Is there documentation for that yet? Or a relatively simple example I can follow? I've read https://twig.symfony.com/doc/3.x/advanced.html#defining-a-node but I can't see what I'm doing differently.
Or is it something that can be fixed in the BC layer?
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, it's working now!
I wasn't sure how to remove the call to twig_ensure_traversable, so I replaced it with the internal call that's used elsewhere:
->write("\$context['_seq'] = CoreExtension::ensureTraversable(\$data);\n")
Is there a better way?
ensureTraversable
That's the way to go indeed.
Seems to be working fine now, thanks!
@fabpot
That's the way to go indeed.
CoreExtension::ensureTraversable
is internal too. How can this be the way to go if using twig_ensure_traversable
, also internal, was not the way to go and was removed without notice - I quote you:
You will also need to remove the usage of the twig_ensure_traversable function as it's an internal function that has been removed.
@fabpot
That's the way to go indeed.
CoreExtension::ensureTraversable
is internal too. How can this be the way to go if usingtwig_ensure_traversable
, also internal, was not the way to go and was removed without notice - I quote you: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 way to go" just means that CoreExtension::ensureTraversable
is the replacement for twig_ensure_traversable
.But to make it clear, this is still internal and not recommended for usage in user space.
@fabpot fair enough. Is there a recommended way?
@fabpot fair enough. Is there a recommended way?
That's really a one liner: return is_iterable($seq) ? $seq : [];
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
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
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.