2
votes

In my symfony project I have a bundle named Modules that included of many modules that each one saved in a separate directory into src/ModuleBundle/Modules directory and they have separate twig files as their templates.
file structure for an sample module named RandomImage

src
   ModuleBundle
       Modules
           RandomImage
               Resources
                   views
                   public

I don't have any problem with loading twig files for each module but my problem is about assetic:dump function that can't find my modules assetic.
Symfony move into all twig files and fetch all assetic files that listed inside {% stylesheets %} and {% javascripts %} block or used in {{ asset() }} function.
In my research i understand that Symfony search and map assetic in two target

  1. assetics listed in base template twig file (e.g. app/Resources/views/base.html.twig)
  2. assetics listed in each one of bundles twig files (e.g. src/myBundle/Resources/views/index.html.twig)


How I can add new target for Symfony to fetch assetic files from their twig files?

1
I think you should create a Module namespace that contains distinct bundles inside (src > Modules > RandomImageBundle), bundle is a synonym of module so your architecture looks really redundant.Alain Tiemblo

1 Answers

1
votes

Assetic has a configuration for which bundle's to search through - assetic.bundles found in config.yml:

assetic:
    bundles:

        # Defaults (all currently registered bundles):
        - FrameworkBundle
        - SecurityBundle
        - TwigBundle
        - ...

Make sure:

  • assetic.bundles is unset or set to ~
  • otherwise make sure your bundle is listed in there
  • your bundle is registered in app/AppKernel.php

In order to change the default ressources of Symfony/AsseticBundle extend ( i.e. via bundle inheritance )

vendor\symfony\assetic-bundle\Symfony\Bundle\AsseticBundle\DependencyInjection\Compiler\TemplateResourcesPass.php

the default Resources/views folder structure can be found around line 50-70 ( as of Symfony 2.3 )

$container->getParameter('kernel.root_dir').'/Resources/'.$bundleName.'/views',
// ...
$bundleDirName.'/Resources/views',
// ...
new DirectoryResourceDefinition('', $engine, array($container->getParameter('kernel.root_dir').'/Resources/views'))

The ResourceDefinitions are being loaded in a CompilerPass - read more about it here.