简体   繁体   中英

Symfony including assets (stylesheets/js) based on the controller

I am trying to find a good way to make my js and css modulo. The thought came to me that when a particular controller is run, it would automatically find a corresponding css/js file and add it. Eg if i run the TodoController, then a TodoController.css file will be automatically loaded.

Has anyone done something like this? If its not really the way to go, then how would one load the assets needed per controller?

The right approach would be loading assets based on the views, resp. logical areas of your project and not on the controllers. The Twig templating system has a powerful inheritance ability, so you can define base templates for specific areas of your project (with specific assets) and then extend these with templates for particular views (aka pages).

You can learn more on this here: http://twig.sensiolabs.org/doc/2.x/templates.html#template-inheritance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM