Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - print_r in twig view

Tags:

php

laravel

twig

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.

like image 229
Tobias Hagenbeek Avatar asked Aug 02 '13 18:08

Tobias Hagenbeek


People also ask

How to dump in Twig?

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.

What does Print_r do in PHP?

Definition and Usage The print_r() function prints the information about a variable in a more human-readable way.


1 Answers

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.

like image 187
SirDerpington Avatar answered Sep 26 '22 00:09

SirDerpington