85

When I am trying to use the TWIG {% javascript %} tag to link to my .js file it return me with the following exception :

An exception has been thrown during the compilation of a template ("You must add CompetitiongameBundle to the assetic.bundle config to use the {% javascripts %} tag in CompetitiongameBundle:game:index.html.twig.") in "CompetitiongameBundle:game:index.html.twig".

My index.html.twig looks like :

{% javascripts 'CompetitiongameBundle/Resources/views/public/js/*'%}
    <script type="text/javascript" src="{{ asset_url }}" ></script>
{% endjavascripts %}
Hello {{ name }}!

<a href='{{ nexturl }}' >Login</a>

My Bundle is already present in the config file when I do :

php app/console config:dump-reference assetic

How can I fix this ?

Antoine Subit
  • 9,803
  • 4
  • 36
  • 52
Manish Basdeo
  • 6,139
  • 22
  • 68
  • 102

4 Answers4

177

Yes I tried and it solved the issue for me. For someone (like me) who doesn't know initially how to add then just:

  1. edit app/config/config.yml
  2. then go to assetic:
  3. under assetic: go to bundles: []
  4. and in bundles: [] //type your bundle name

for instance if your bundle is Acme\DemoBundle, then do the following

assetic:
   bundles: [ AcmeDemoBundle ]

No quotes around AcmeDemoBundle. That's it. (Symfony2)

7ochem
  • 2,183
  • 1
  • 34
  • 42
Shabbir Reshamwala
  • 1,886
  • 1
  • 12
  • 3
  • 14
    If needed, additional bundles should be separated by commas. – Zoot Feb 11 '14 at 20:33
  • 1
    I'm using symfony2.3, I added my bundle to the array but I still have the same exception. Even when I comment the bundles I get the same exception message. What should I do? – Dev DOS May 16 '15 at 11:10
  • If you are migrating to prod, and have this issue, don't forget to clean your cache and dump your assetic assets : " php app/console cache:clear --env=prod --no-debug " and " php app/console assetic:dump --env=prod --no-debug " – Bertrand Nov 01 '15 at 09:17
  • @Dev DOS do u found a solution .. ? – NizarETH May 20 '16 at 16:23
24

If you want assetic to include your bundles by default, you can comment (with #) the line bundles: []

ex:

assetic:
    debug:          "%kernel.debug%"
    use_controller: false
    #bundles:        [ ]
    #java: /usr/bin/java
Tivie
  • 18,864
  • 5
  • 58
  • 77
  • are there any side effect for commenting bundles? – Permana Oct 21 '13 at 05:49
  • None that I can think of. It just makes assetic include all bundles in your project. Maybe it can slow down your app, but personally I never noticed any differences in speed. – Tivie Oct 21 '13 at 21:08
10

Sometimes you need to make decisions on the fly, then you can use use DependencyInjection.

For example to loads and manages configuration:

<?php

namespace You\ExampeBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;

/* ... */

class YouExampeExtension extends Extension
{

    /* ... */

    public function load(array $configs, ContainerBuilder $container)
    {
        /* ... */

        $aAsseticBundle = $container->getParameter('assetic.bundles');
        $aAsseticBundle[] = 'YouExampeBundle';
        $aAsseticBundle[] = 'AnotheBundle';
        $container->setParameter('assetic.bundles', $aAsseticBundle);

        /* ... */
    }
}

You can use more complex logic to manipulate the configuration(in reasonable limits)

  • 1
    This is particularly useful if you extend another Bundle using Bundle inheritance. – flu Dec 11 '15 at 13:09
3

You need to add your bundle to bundle: [] row of assetic: section in app/config/config.yml file (symfony 2.1)