$sf_response->addStyleSheet() dosen't work in

2020-04-26 00:58发布

问题:

Does anyone know how to add stylesheets in a template with Symfony 1.4 ?

I have tried everything I can think of, from modifying frontend/config/view.yml to modifying the template itself - bothing works.

I have seen from my searches, that other people have had the same problem. There seems to be a clash of sorts between using include_stylesheets and use_stylesheets - however this is not documented anywhere AFAIK.

回答1:

Edit:

Ok I think I got it now. You should add include_stylesheets() into the head section of your layout file:

<html>
   <head>
       <title>This is the title</title>
       <?php include_stylesheets() ?>
   </head>
   <body>
   <!-- ... -->

Then in your template file, you use use_stylesheet() to add a particular stylesheet for this template:

<?php use_stylesheet('/path/to/stylesheet.css') ?>

From the API documentation:

include_stylesheets()
Prints <link> tags for all stylesheets configured in view.yml or added to the response object.

use_stylesheet()
Adds a stylesheet to the response object.

Same for Javascript.


According to the API documentation it should still work in 1.4, sfWebResponse still has this method:

addStylesheet ($file, $position, $options)   
  $file   The stylesheet file  
  $position   Position   
  $options    Stylesheet options  
Adds a stylesheet to the current web response.

At least the method exists.
What exactly is the problem? Do you get an error if you want to call that method or is the stylesheet just not added?



回答2:

http://www.symfony-project.org/tutorial/1_4/en/upgrade#removal_of_the_common_filter

As of 1.4 your javascripts and stylesheets are no longer automatically injected into your head tag. Instead, you need to include the following in your layout where you'd like them to be placed:

<?php include_javascripts() ?>
<?php include_stylesheets() ?>

and just in case your post title wasn't a typo you'll want to use addStylesheet('...') off of the response:

$sf_response->addStylesheet('main');


回答3:

$sf_context->getResponse()->addStylesheet('style.css')

$sf_context->getResponse()->addJavascript('script.js')