I have a "simple" question I would hope, and that is how can I print_r or at least see the contents of all defined variables in a twig file.
I have tried: {{ variable }}
(where variable is an array set for the view
$viewData['variable'] = array('1','2','3');
in the controller.
I have also tried: {{ $variable }}
That gives an error.
I would just want to know what is available from my array in the twig file.
In a Twig template, you can use the dump utility as a function or a tag: {% dump foo. bar %} is the way to go when the original template output shall not be modified: variables are not dumped inline, but in the web debug toolbar; on the contrary, {{ dump(foo.
Definition and Usage The print_r() function prints the information about a variable in a more human-readable way.
You could use the built in {{ dump() }}
function. See the documentation.
If you use it without any value in the brackets it will dump all variables available. For dumping only your array you would do it like this:
{{ dump(viewData) }}
With something like xdebug the output looks quite nice and is readable.
array (size=3)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '3' (length=1)
Although the documentation says it's not available by default it was added in twig 1.5 and should be ready to use by default.
Of course not the same as print_r
but with xdebug enabled it outputs nice and readable var_dump
information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With