Step by Step Guide to Enabling Twig Debug dump() in Symfony - symfony

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.

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 %}

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

Related

FOSUserBundle with Symfony 3.0

I'm trying to get FOSUserBundle up and running after upgrading Symfony from version 2.6 to 3.0. On 2.6 I basically had a site, css and js we're handles by assetic, FOSUserBundle was up and running an I had configured SQLite via doctrine.
So before picking this project up again I did the upgrade because 2.6 was no longer supported.
Because I'm new to Symfony everything I did is more or less copied from the documentation. FOSUserBundle is showing up as an enabled bundle in dev-mode. Alle my code is in /src/AppBundle. I'm using the 2.0-dev version of FOSUserBundle (e770bfa to be precise).
Here's a part of my template:
<li>
{% if is_granted("IS_AUTHENTICATED_REMEMBERED") %}
<a href="{{ path('fos_user_security_logout') }}">
{{ 'layout.logout'|trans({}, 'FOSUserBundle') }}
</a>
{% else %}
<a href="{{ path('fos_user_security_login') }}">
{{ 'layout.login'|trans({}, 'FOSUserBundle') }}
</a>
{% endif %}
</li>
At 2.6 I got
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.yml"
in my /src/AppBundle/config/routing.xml. If I keep it this way I can't even load the site because of an Exception
"Unable to generate a URL for the named route "fos_user_security_login" as such route does not exist."
After putting it in /app/config/routing.yml my site shows and the other routes defined in /src/AppBundle/config/routing.xml work. So it was nothing wrong with this file.
When the site shows up the translation of the links is broken.
{{ 'layout.login'|trans({}, 'FOSUserBundle') }}
Shows up as "layout.login" instead aus Login as before with 2.6. And if I click the link to log in (the path is as expected /login). Symfony tells me:
Unable to find template "AppBundle:Pages:login.html.twig".
I don't get why it's looking for it in the AppBundle folder? According to what I read it should look for it in the FOSUserBundle folder or in /app/Ressources/FOSUserBundle/... which is where I put it to override the default template. I confirmed that this is still the way to override templates even with version 3.0.
I also tried to put the template in the AppBulde/Pages/ folder. Then it finds the template but it's still not working.
I cleared the cache multiple times (this solved another problem with assetic).
It look for me as if I'm missing a significant part to get FOSUserBundle working. Any suggestions on what I might have overlooked?
FOSUserBundle seems to be use 'xml' files for routing, and you have include an 'yml' file in your routing.xml:
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.yml"

Symfony Assetic featurejust seem not to work

Current solutions to this question don't work...
I can't seem to get Symfony assetic to function. I have the following configured in my twig template
{% javascripts
"/js/foundation/foundation.abide.js"
"/js/foundation/foundation.reveal.js"
"/js/edged/jquery-ui.form.js"
"/js/edged/jquery-ui.table.js"
"/js/jquery.autocomplete.js"
"/js/jquery.typewatch.js"
output="/js/combined.js"
%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
For same reason in development environment with use_controller = true I get the following error
An exception has been thrown during the rendering of a template
("Unable to generate a URL for the named route "_assetic_476d03f_0" as
such route does not exist.") in admin/view.html.twig at line 314.
When I set use_controller to false and do the whole assetic dump
app\console assetic:dump
The assets are not found, after digging around I noticed that the the _assetic route configurations have been removed from version 2.6 and 2.7, I think as of version 2.4 adding the configuration doesn't resolve any of the issues am facing.
Such a cool feature and I can;t get it working :(
Verify in your config.yml file under Assetic Configuration that you have the bundle registered.
assetic:
debug: "%kernel.debug%"
use_controller: false <-- this should be set to true in config_dev.yml
bundles:
- AppBundle <-- your bundle name

How to enable Twig's dump() in Symfony2

Seeing various threads on this topic but not finding a working answer. Have a simple Symfony2 app (2.3.5) and trying to dump variables passed into my Twig templates. I have in my app/config/config.yml:
# Twig Configuration
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
and in my app/config/config_dev.yml:
services:
twig.extension.debug:
class: Twig_Extensions_Extension_Debug
tags:
- { name: twig.extension }
But using dump() in a twig still renders an empty page. I also increased memory limit in php.ini to 512 ... still nothing
Which part of this am I missing?
Try class: Twig_Extension_Debug instead. :)
In symfony2.3, this extension is automatically enabled when twig.debug is set to true, so you should be able to use the dump function.
Try {% debug var %} as shown in this link:
http://www.craftitonline.com/2011/06/symfony2-debugging-with-twig-extensions/
Because I didn't found the answer on the web, I will share my working solution here:
# app/config.yml
twig:
debug: %kernel.debug%
Then in a Twig template:
{% dump var %}
Tested on Symfony 2.7

assetic:dump fails on "less" filter

When I try and dump my assetic managed content for a Symphony2 app, I get the following error:
$ php app/console assetic:dump -e prod
Dumping all prod assets.
Debug mode is off.
[InvalidArgumentException]
There is no "less" filter.
However, as far as I can tell I don't use any assets that require the less filter, certainly a grep of all my twig templates doesn't turn anything up.
One of the dependencies I've installed via composer (Twitter's bootstrap) has some .less templates, but I don't reference them in my twig templates, I just point it at the css version. Will assetic still try and dump them? How can I tell it not to?
For reference this is how I include the css in my template
{% stylesheets filter="cssrewrite"
'../vendor/jquery-ui-css/jquery-ui-css/*css'
'../vendor/twitter/bootstrap/docs/assets/css/bootstrap*.css'
%}
<link rel="stylesheet" type="text/css" href="{{ asset_url }}">
{% endstylesheets %}
hm many possible causes ...
1) some third-party bundle adds an assetic collection dependeing on the less filter in a compiler pass
2) there is an apply-to rule like
assetic:
filters:
less:
apply_to: *.less
... in your assetic configuration i.e. app/config/config.yml
3) there is an asset collection using less filter in your configuration
assetic:
assets:
css_character:
inputs:
- "%kernel.root_dir%/../src/Acme/YourBundle/Resources/public/less/*.less"
outputs:
- css/my.css
filters:
- less
4) one of your third-party bundles provides a twig template using assetic's {% stylesheets %} function with the less filter:
{% stylesheets "#AcmeTwitterBundle/Resources/bootstrap/less/*.less" filter="less" %}
{# ... {{ asset_url }} ...#}
{% endstylesheets %}
Now how to find out?
At first check your config files app/config/config.yml and other included ones for assetic entries using the less filter.
The easiest way to find out where the less filter is being used is installing ElaoWebProfilerExtraBundle, clearing your cache and having a look at the "Assetic" tab in the left-side menu of the profiler. You will get an overview of all assetic collections and the filters they use.
Another option - not involving a new bundle although WebProfilerExtraBundle is awesome - is to disable your third-party bundles one by one ( and clear the cache each time ) in app/AppKernel.php try if assetic:dump still throws the exception until you find the bad-boy.
Or dirty: enable the filter though less is probably not installed and see where the next exception is thrown:
assetic:
filters:
less: ~
... all in all i would bet you included the supercool mopa-bootstrap bundle of which almost nobody knows what it's actually doing behind the scenes but it's famous and many people install it because of it's KnpBundle's score.
meaning ... your exception would then be thrown because of the less files included in the templates provided by MopaBootstrapBundle i.e. here.

Configure output dir for Assetic in Symfony2

I'd like to globally configure the output dir of where assetic dumps my JS files. Currently, they always go to web/js/*. I want to change this to web/js/compiled/*.
It's possible to specify this at a per-file level: http://symfony.com/doc/2.0/cookbook/assetic/asset_management.html#dumping-asset-files
Can't seem to find a way to set this globally across my Symfony app. Any config parameter I'm missing?
UPDATE
Found an assetic config parameter called write_to. Setting this in config.yml causes the command line assetic:dump to dump files to the new dir, but within twig files the asset_url var still points to the original path.
You should use the property write_to.
in my configuration for exemple I use
# Assetic Configuration
assetic:
debug: %kernel.debug%
use_controller: %kernel.debug%
read_from: %kernel.root_dir%/Resources/views/
write_to: %kernel.root_dir%/../web/static/
Your ouput string start where ends write_to
for exemple
{% javascripts filter="closure" output='js/main.js'
...
{% stylesheets filter='compass,?cssrewrite'
'default/static/sass/screen.scss'
output='css/screen.css'
%}
both will placed respectively in /web/static/js/main.js
and /web/static/css/screen.css
assets_base_urls is used to specify base URL's to be used for assets referenced from http and ssl (https) pages.
!! assets_base_urls is also used by {% images %} as the root before output value, but {% images %} doesn't consider write_to when rendering html (only when dumping) so better not using write_to and rely only on output value. More about it in my other post on stackoverflow and in this post on AsseticBundle's github.
You can set the asset path ( assets_base_urls ) for twig to a static path, instead of using the relative path. In your config.yml file, it would look similar to this:
framework:
templating:
engines: ['twig']
assets_base_urls:
http: [http://path.to-cdn.com]
This will effect asset_url from assetic as well as twig's asset() method. The latter may or may not be desired.
This GitHub issue comment helped me with this issue.
While in dev, your assets will still go thru the controller but in production, the URLs will be as you desire.
Example config.yml:
assetic:
write-to: %kernel.root_dir%/../web/assets
...
framework:
...
templating:
engines: ['twig']
packages:
assetic:
base_urls: '/assets'
Example in your template:
{% block javascripts %}
{% javascripts '#jquery' '#bootstrap_js' '#backbone' '#handlebars' combine=true package='assetic' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
Notice that you have to add the package='assetic' attribute in the java scripts tag. This is a good compromise IMO because it won't break assets from other bundles as kmfk's solution will.
Just a quick note on this. If you're using assets_base_urls, to specify a relative base URL, this only works prior to Symfony 2.7, due to the introduction of the new assets component in that version. Further information on how to change this is available at http://symfony.com/blog/new-in-symfony-2-7-the-new-asset-component , but the long and short of it is that:
framework:
templating:
assets_base_urls:
http: ['/some-relative-url']
ssl: ['/some-relative-url']
becomes:
framework:
assets:
base_path: /some-relative-url
Try this commande $ app/console --env=prod assetic:dump web/
you have juste to change the url you want raher than 'web/'

Resources