Does anyone know how to loop through all parameters that are passed to a Twig template without knowing in advance what they are called?
The {{ dump() }}
function (which calls var_dump()
) outputs something like this:
array(5) {
["foo"]=>
bool(true)
["bar"]=>
string(3) "Yes"
["baz"]=>
int(99)
["subdata1"]=>
array(1) {
["foo2"]=>
bool(false)
}
["subdata2"]=>
array(1) {
["foo3"]=>
int(5)
}
}
I want to loop through all parameters which are not subdata1
or subdata2
, so that I can output something like:
foo is true
bar is Yes
baz is 99
Preserving the data structure sent to the template is important, so I am looking for a solution on the Twig side of the pipe.
For the past two days I have trawled through the sparse Twig documentation trying to find a hidden gem which reveals how to do this, but turned up nothing.
before
the parameters are given to the template? Twig uses theoutput buffer
to provide nesting functionality, so naturally it makes no difference whether the parameters are manipulated before or after the rendering. – Flosculus