How to enable Twig's dump() in Symfony2 - symfony

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

Related

AsseticBundle Configuration - Explaination

there is no real detail in the documentation about the AsseticBundle Config - in config.yml.
assetic:
debug: "%kernel.debug%"
use_controller:
enabled: "%kernel.debug%"
profiler: false
read_from: "%kernel.root_dir%/../web"
write_to: "%assetic.read_from%"
java: /usr/bin/java
node: /usr/bin/node
ruby: /usr/bin/ruby
sass: /usr/bin/sass
# An key-value pair of any number of named elements
variables:
some_name: []
bundles:
# Defaults (all currently registered bundles):
- FrameworkBundle
- SecurityBundle
- TwigBundle
- MonologBundle
- SwiftmailerBundle
- DoctrineBundle
- AsseticBundle
- ...
assets:
# An array of named assets (e.g. some_asset, some_other_asset)
some_asset:
inputs: []
filters: []
options:
# A key-value array of options and values
some_option_name: []
filters:
# An array of named filters (e.g. some_filter, some_other_filter)
some_filter: []
workers:
# see https://github.com/symfony/AsseticBundle/pull/119
# Cache can also be busted via the framework.templating.assets_version
# setting - see the "framework" configuration section
cache_busting:
enabled: false
twig:
functions:
# An array of named functions (e.g. some_function, some_other_function)
some_function: []
I'm specially interested in
read_from: don't understand the path, too
write_to:
because I don't really understand how to use it.
So, I want to use SCSS and Compass and I have an folder in AppBundle/Resources/assets/styles/main.scss
What I have to setup in the config.yml, that assetic know how he find the main.scss as a global setting?
Unless you are trying to update the directory from which Assetic reads/writes (thus being /web by default), you don't need to change anything here. The configuration can be understood from a good part on the Symfony documentation. You'll find what you need in:
read_from: "%kernel.root_dir%/../web"
write_to: "%assetic.read_from%"
These are paths to a directory which is writable/readable, and exposed to the public. In this case, it means it will look for /path/to/app/../web for both readings and writings.
In a general manner, check for php app/console config:dump-reference X to find the default configuration of a given bundle, where X is the bundle config name. In your case, try the later: php app/console config:dump-reference assetic
Now, what you want is to use compass/sass from your view as far as I can see.
In your twig file, put the following:
{% stylesheets 'path/to/main.scss' filter='compass' %}
<link rel="stylesheet" type="text/css" href="{{ asset_url }}">
{% endstylesheets %}
After adding the configuration for compass if it needs to be tweaked, you should be all set.
Is it helping? it not, could you please provide more details?

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

Unrecognized option "resources" under "twig.form"

I didn't touch my project since a week and today I have this error when I want to go on my website
Unrecognized option "resources" under "twig.form"
I don't know why.. This was working very fine before...
Here is my config.yml :
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
globals:
breadcrumb_trail: "#apy_breadcrumb_trail"
form:
resources:
- 'AppBundle:Form:fields.html.twig'
If you have any idea.. :)
If you are on SF3 or higher, check that the yaml definition hasn't changed to specify a form template: the current one (3.0.*) should be like this:
twig:
form_themes:
- 'bootstrap_3_layout.html.twig'
source

Step by Step Guide to Enabling Twig Debug dump() in 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

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