mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-30 20:16:45 +00:00
Allow Traversable objects to be used in the with tag
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
* fixed the spaceless filter so that it behaves like the spaceless tag
|
||||
* fixed BC break on Environment::resolveTemplate()
|
||||
* fixed the bundled Autoloader to also load namespaced classes
|
||||
* allowed Traversable objects to be used in the "with" tag
|
||||
|
||||
* 1.38.2 (2019-03-12)
|
||||
|
||||
|
||||
@@ -40,6 +40,11 @@ class WithNode extends Node
|
||||
->write(sprintf('$%s = ', $varsName))
|
||||
->subcompile($this->getNode('variables'))
|
||||
->raw(";\n")
|
||||
->write(sprintf("if (\$%s instanceof \\Traversable) {\n", $varsName))
|
||||
->indent()
|
||||
->write(sprintf("\$%s = iterator_to_array(\$%s);\n", $varsName, $varsName))
|
||||
->outdent()
|
||||
->write("}\n")
|
||||
->write(sprintf("if (!is_array(\$%s)) {\n", $varsName))
|
||||
->indent()
|
||||
->write("throw new RuntimeError('Variables passed to the \"with\" tag must be a hash.');\n")
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
--TEST--
|
||||
"with" tag with an iterable expression
|
||||
--TEMPLATE--
|
||||
{% with vars %}
|
||||
{{ foo }}{{ bar }}
|
||||
{% endwith %}
|
||||
--DATA--
|
||||
return ['vars' => new ArrayObject(['foo' => 'baz', 'bar' => 'qux'])]
|
||||
--EXPECT--
|
||||
bazqux
|
||||
Reference in New Issue
Block a user