views:

295

answers:

3

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.

+4  A: 

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');
Cryo
That's right if someone wants to add them to layout
Darmen
Actually those methods can be called from any template file, not just the layout.
Cryo
Yes, you're right
Darmen
@Cryo, Darmen: It is not totally correct because the `include_stylesheets()` command prints out `link` tags, which are not allowed in the `body` tag. Actually `link` tags in the body only work if a full URL is used. Of course it works it you define a slot in the `head` section and populate that slot with this output in a template. See also: http://archivist.incutio.com/viewlist/css-discuss/74331
Felix Kling
A: 

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

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

Darmen
+1  A: 

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?

Felix Kling
it seems that poster doesn't know ho to add stylesheets dynamically in templates of 1.4
Darmen
Hello Felix - good to hear from you again (long time!). The problem is related to the common filter which has been removed from the filters.yml in 1.4. There are two problems related with this issue, even though I asked only about stylesheets. This issue also affects the way javascripts are added to the page (just before the end of the closing body tag) - which means if you are using jQuery (for example), you get errors like jQuery is not defined (because the jQuery logic in the template is encountered before the jQuery library is loaded - but that is another problem for another day) ...
Stick it to THE MAN
The new change breaks my existing pages (for javascript related issues, this only affects pages that are using jQuery). Back to THIS question, (regarding stylesheets), as I mentioned, I tried EVERYTHING mentioned in the documentation (which by the way contradicts itself) - In one doc (both related to the 1.4 version) it recommends to use view.yml and include_stylesheets() and in another it recommends use_stylesheet() - I'm suprised no one has spotted this. Anyway, I tried both methods (yes, and cleared the cache too) - NONE WORKED. I then tried $sf_response->addStyleSheet()
Stick it to THE MAN
$sf_response->addStyleSheet() DID NOT WORK EITHER, in my disgust (after wasting two days on this), I downgraded down to 1.3. Unless I am wrong (and I will be MORE THAN HAPPY to be proved wrong), I dont think 1.4 is really suitable for production yet - since it seems to be 'features' over 'common sense'. I wont bother with 1.4 for a long time, until it has bedded down, and all these 'issues' have been sorted out.
Stick it to THE MAN
Felix: apologies for the (slight?) rant above. To directly answer your question, when I say "it dosen't work", I mean the CSS file(s) is/are NOT added to the generated view (output)
Stick it to THE MAN
Stick it to THE MAN: I would suggest starting from a fresh project on 1.4. I've been able to successfully use both the include_stylesheets method I mentioned above and also get your jQuery code working from your other question here: http://stackoverflow.com/questions/2093637/jquery-is-not-defined-in-symfony-templating-environment off of a clean 1.4 installation. It sounds like something else is in the mix of your current project that's causing your issues. Hope that helps.
Cryo
Cyro: yes, I think thats the best approach. The current proj will get launched using 1.3. I'll do the upgrade to 1.4 later. Other projects will start off 1.4
Stick it to THE MAN
@Stick it to THE MAN: What about my updated answer? Does it help you?
Felix Kling
Felix: I have not had the time to test (and unfortunately, am unlikely to be able to test it soon). As I mentioned earlier, I spent 2 days with this problem, before given up and going back to 1.3. I am pushed for time, so will not be able to investigate this (for this project), but will do so for other project scheduled in a few months time.
Stick it to THE MAN
I decided to try as you suggested (admittedly on SF 1.3) - and it worked. The same problem was occuring on SF1.3, but I got around it by enabling the sfCommonFilter (which I wasn't happy with - since it meant I would have had to deal with the issue again when moving 1.3 -> 1.4). What can I say?. I stand in utter and complete awe of you "Symfony fu". :)
Stick it to THE MAN
Thank you ;) ... Someone voted down, I really would like to know why.
Felix Kling