How components should add javascript and css to symfony

My layout.php calls include_javascripts () before my component can call sfResponse :: addJavascript (). Is there a โ€œhelperโ€ or โ€œbest practiceโ€ for this?

Do I need to cancel the call to sfResponse :: addJavascript ()? I was happy to avoid this.

Here is my actual workaround:

<head> <?php $nav = get_component('nav', 'nav') /* Please show me a better way. */ ?> <?php include_javascripts() ?> ... </head> <body> <?php /* include_component('nav', 'nav') */ ?> <?php echo $nav ?> ... </body> 

thanks

+4
source share
3 answers

If your component is always used, just move the inclusion of javascript into the rendering layout in your view.yml application:

 default: javascripts: [my_js] 

There is no need to separate the JS call when it is always in use.

UPDATE:

If you need to support the inclusion of JS with a component, you can put your component call in the slot before calling include_javascripts() to add it to the stack that will be displayed, and then enable the slot in the right place:

 <?php slot('nav') ?> <?php include_component('nav', 'nav'); ?> <?php end_slot(); ?> <html> <head> <?php include_javascripts() ?> ... </head> <body> <?php include_slot('nav'); ?> <?php echo $nav ?> ... </body> </html> 
+3
source

From: http://www.symfony-project.org/book/1_2/07-Inside-the-View-Layer

File Inclusion Configuration

// In view.yml

 indexSuccess: stylesheets: [mystyle1, mystyle2] javascripts: [myscript] 

//In action

 $this->getResponse()->addStylesheet('mystyle1'); $this->getResponse()->addStylesheet('mystyle2'); $this->getResponse()->addJavascript('myscript'); 

// In the template

 <?php use_stylesheet('mystyle1') ?> <?php use_stylesheet('mystyle2') ?> <?php use_javascript('myscript') ?> 
+11
source

You may need to disable sfCommonFiter , which is now deprecated . The general filter automatically adds css / js to the layout.

Once you do this, you can move the include_javascripts call anywhere in the layout.php file below the include_component('nav', 'nav') call

0
source

Source: https://habr.com/ru/post/1301186/


All Articles