Reputation: 7331
The big goal is to just print out the variables that are available in the twig form template /views/Form/fields.html.twig so that I can find out WHICH variables are available, AND in particular put a condition in the {% block widget_attributes %}
based on the field type (which supposedly exists but for some reason is not accessible, and other suggestions to get the type are warned against).
I just want to see all the variables that are available... and the values they hold. Easy, right?
So that led me down a lot of rabbit holes, and some helpful articles point out how you can loop through the variables of the current context:
{% block widget_attributes %}
<ol>
{% for key, value in _context %}
<li>{{ key }} :
{% if value is not iterable%}
{{ value }}
{% else %}
{{ dump(value) }}
{% endif %}
</li>
{% endfor %}
</ol>
{% set attr = attr|merge({'class': (attr.class|default('') ~ ' form-control')|trim}) %}
{{ parent() }}
{% endblock widget_attributes %}
But this doesn't print out type
, and it doesn't actually dump the value if it's not iterable. It kills symfony without any error. So getting debug to work is essential for many reasons.
All the suggestions to enable dump, don't work. Twig's website is particularly unhelpful, since it provides no context how or where to load the $twig = new Twig_Environment (and what is up with the latest version being 1.5 at twig but 1.16 in symfony?). Symfony says it will be enabled by default. But it doesn't work.
The app.php (to load the kernel has debug enabled):
$kernel = new AppKernel('dev', true);
This is what is in my config.yml:
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
And the other suggestions for enabling in the config_dev.yml don't work either:
imports:
- { resource: config.yml }
# this is from one of the suggestions, but it doesn't work and may be an older method
services:
twig.extension.debug:
class: Twig_Extension_Debug
tags: [{ name: 'twig.extension' }]
So as with so many thing in Symfony, it's powerful and awesome, until it doesn't work, and then there is no documentation on how to make it work. Any help would be appreciated.
I'm running Symfony 2.5, which composer updates to Twig 1.16.
Upvotes: 3
Views: 6348
Reputation: 1342
If you try to enable it on prod env.
You should add following into app/AppKernel.php;
$bundles[] = new \Symfony\Bundle\DebugBundle\DebugBundle();
and in app/config.yml edit this line;
twig:
debug: %kernel.debug% ========> sure this set true
strict_variables: %kernel.debug%
but keep in mind if your project in live server it can be insecure in terms of leaking debug information
Upvotes: 1
Reputation: 7331
All the suggestions I read in other posts seemed to be for an older version of Symfony and they did not work for me. But Twig debugging is enabled by default in Symfony now. So this is what I did to solve my problem:
1. Upgrade to Symfony 2.5. Edit the /composer.json file and update the symfony version.
2. Update your required dependencies. On the command line run composer update
3. Update Twig. This also automatically updated twig to 1.16 (Symfony requires a minimum version, so if your project requires Twig's latest version of 1.5 you need to require that in our own composer.json
file).
4. Load Kernel with Debug On. Make sure the kernel loads with debug turned on in Dev mode, by default this would be in your app_dev.php file (the index file loaded to access your dev mode).
$kernel = new AppKernel('dev', true);
5. Check Config. Make sure twig debug is enabled based on kernel debug mode, edit the config.yml:
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
6. Check Dev Config. Make sure your config_dev.yml imports config.yml (or at least has the relevant config from above).
imports:
- { resource: config.yml }
After doing that, the dump function now works in Twig:
{% block widget_attributes %}
{{ dump(attr) }}
{% set attr = attr|merge({'class': (attr.class|default('') ~ ' form-control')|trim}) %}
{{ parent() }}
{% endblock widget_attributes %}
Upvotes: 2