3

Goal

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?

Lost

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.

Enabling Dump

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' }]

Still Lost

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.

Community
  • 1
  • 1
Chadwick Meyer
  • 7,041
  • 7
  • 44
  • 65
  • Check accepted answer in this question: [Link](http://stackoverflow.com/questions/12159373/the-function-dump-does-not-exist-in-twig-file) – waldek_h Oct 01 '14 at 09:20
  • @waldek_c Thanks for the link. None of these answers worked, but in the process I somehow fixed it (see my answer below). – Chadwick Meyer Oct 01 '14 at 22:13
  • However, The bigger problem still remains, you can't `dump(_context)` that ends up with a white page. Maybe this happens when you dump a recursive object maybe? So I have no idea how to actually see all the variables available in twig... back to square one. – Chadwick Meyer Oct 01 '14 at 22:15

2 Answers2

2

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 %}
Chadwick Meyer
  • 7,041
  • 7
  • 44
  • 65
  • 1
    But this STILL doesn't allow me to dump most variables, i.e. if they are an object (presumably). In most cases, that is the REASON I need to do a dump in the first place. But this dump feature just makes the whole page turn white without any errors (others say this may be a memory leak). – Chadwick Meyer Oct 03 '14 at 19:06
  • > Others say this may be a memory leak That's the case for me! – Steven Apr 01 '15 at 10:30
1

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

Tuncay Elvanagac
  • 1,048
  • 11
  • 13