Allow Traversable objects to be used in the with tag

This commit is contained in:
Chris Wilkinson
2019-03-19 14:21:09 +00:00
parent f0efbc5b20
commit 572a2a09fd
3 changed files with 16 additions and 0 deletions
+1
View File
@@ -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)
+5
View File
@@ -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