Custom scripts and styles for widgets in Symfony: position
My sample wi开发者_如何学Godget:
class myWidget extends sfWidgetFormTextarea{
public function getJavascripts(){
return array('my_widget.js');
}
public function getStylesheets(){
return array('my_widget.css');
}
}
Form template:
<?php include_javascripts_for_form($form) ?>
<?php include_stylesheets_for_form($form) ?>
<?php echo $form;?>
The problem is that those 2 functions produce link
and script
tags right before rendering form.
How can I move them to the head
section?
In a template you should use
<?php use_javascripts_for_form($form) ?>
<?php use_stylesheets_for_form($form) ?>
Note that it is use
instead of include
.
See also this question and the documentation.
It is a better approach then accessing the response object directly in this case.
Try adding the .css and .js to your module's view.yml instead of in the template:
formSuccess:
stylesheets: [my_widget]
javascripts: [my_widget]
That will get parsed and dumped into your head tag.
If you have access to the current context's response you can call addJavascript or addStylesheet on the response to inject them into the head. From an action:
$this->getContext()->getResponse()->addStylesheet('style');
From a template:
$sf_response->addJavascript('script');
精彩评论