Is it possible to retrieve all variables inside a Twig template with PHP?
Example someTemplate.twig.php:
Hello {{ name }},
your new email is {{ email }}
Now I want to do something like this:
$template = $twig->loadTemplate('someTemplate');
$variables = $template->getVariables();
$variables should now contain "name" and "email".
The reason I want to do this is that I am working on a CMS system where my twig templates and variables are dynamically set by my users and they also fill the variables through an API.
I want to set default values to not-set variables and therefore I need a list of all variables that exist inside the template…
You have to parse the template, and walk through the AST it returns:
This Question has a douplicate – there I found a useful and kind of more powerfull RegEX than above. This one, I've improved to match more precise:
Answer added at 2015
In the past it wasn't possible. But since version 1.5 dump() function has added. So you can get all variables from current context calling dump() without any parameters:
However, you must add the Twig_Extension_Debug extension explicitly when creating your Twig environment because
dump()
isn't available by default:If you have been using something like Symfony, Silex, etc,
dump()
is available by default.EDIT:
One can also reference all variables passed to a template (outside the context of
dump()
), using the global variable_context
. This is what you were looking for. It is an array associating all variable names to their values.You can find some additional info in the Twig documentation.
For this specific question however, it would probably be best to gather all of these custom variables you are speaking of, under a same umbrella variable, so that retrieving them would not be a headache. I would be an array called
custom_variables
or whatever.After I spent quite a night, trying all the above answers, I realized, for some unexpected reason, regexps did not work at all with my simple templates. They returned junk or partial information. So I decided to go by erasing all the content between tags instead of counting tags ^_^.
I mean, if a template is
'AAA {{BB}} CC {{DD}} {{BB}} SS'
, I just add'}}'
in the beginning of the template and'{{
in the end.... and all the content between}}
and{{
I'll just strip out, adding comma in between =>}}{{BB,}}{{DD,}}{{BB,}}{{
. Then - just erase}}
and{{
.It took me about 15 min to write and test.... but with regexps I've spent about 5 hrs with no success.
Hope it'll help somebody :)
I think 19Gerhard85's answer is pretty good, although it might need some tweaking because it matched some empty strings for me. I like using existing functions where possible and this is an approach mostly using twig's functions. You need access to your application's twig environment.
And the only custom part of it is the recursive function to collect only certain types of nodes:
Create a Twig_Extension and add a function with the needs_context flag:
The context will be passed as first parameter to your function, containing all variables.
On your Twig template you just have to call that function:
If you need assistance on creating a Twig Extension, please refer to this documentation:
http://twig.sensiolabs.org/doc/2.x/advanced.html