Studiosus
Studiosus

Reputation: 143

Get Required Variables From A Twig Template

I'm new with Twig and i need function, which tests if all required variables in a Twig templates are set in an array I searched for a method to get all variables from a twig template, but it seems that this does not exist.

Is there any good solution for that?

Upvotes: 2

Views: 585

Answers (2)

Alain
Alain

Reputation: 36994

The special _context variable contains all variables available in your template.

If you manually want to check what's inside, use:

{{ dump(_context) }}

If you automatically want to check if your variables exists, you can do something like (if your variables are a, b, c):

{% _context|keys|sort|join(',') == 'a,b,c' %}

See the demo

Upvotes: 1

zbigu
zbigu

Reputation: 21

Mayby this function help you - dump() http://twig.sensiolabs.org/doc/functions/dump.html If you work with Symfony you must have this function by default. But if you have only a Twig the dump function is not available by default and You must add the Twig_Extension_Debug extension.

Upvotes: 0

Related Questions