How to Retrieve All Variables from a Twig Template

How to retrieve all Variables from a Twig Template?

UPDATE 2019

Although {{ dump() }} does work, in some circumstances it may result in a "memory exhausted" error from PHP if it generates too much information (for example, due to recursion). In this case, try {{ dump(_context|keys) }} to get a list of the defined variables by name without dumping their contents.

UPDATE 2017

It is possible by using {{ dump() }} filter. Thanks for pointing that out in the comments!


OUTDATED

It is not possible.

You can look for these variable in twig templates and add |default('your_value') filter to them. It will check if variable is defined and is not empty, and if no - will replace it with your value.

How to var_dump variables in twig templates?

As of Twig 1.5, the correct answer is to use the dump function. It is fully documented in the Twig documentation. Here is the documentation to enable this inside Symfony.

{{ dump(user) }}

can I display parameters value ($_POST, $_GET, $_SESSION, $_SESSION) using twig template engine component

The only variables available in Twig are:

  • the ones you pass through the second parameter of Twig_Environment#render()
  • the ones you pass by calling Twig_Environment#addGlobal()

If you wish to have a page variable available, add "page" => $_GET["page"] to the second parameter of render.

If you wish to have the complete $_GET superglobal available, add "GET" => $_GET to the second parameter of render.



Related Topics



Leave a reply



Submit