Laravel - print_r in twig view

laravel, php, twig

Solution

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.

Problem

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.

Original source