Linking CSS stylesheet on Zend Framework specific pages

Every so often, at the beginning of a new project at work, I almost always have to add a page-specific stylesheet to a certain page on the project. And usually (almost always it seems) I forget the exact code to enter at the top of the .phtml page.

And I thought I should blog it, so that at least I remember it for future use…

This is for Zend Framework-built sites only, of course, and so long as this is in your <head> section of your bootstrap Index file…

<?php
echo $this->headTitle();
echo $this->headScript();
echo $this->headStyle();
echo $this->headLink();
?>

…Then, on the page we’re talking about, before output on your .phtml file, add the following code

$this->headLink()->appendStylesheet('/path/to/page-styles.css');

Done! So there you are. Some of you might have already known that, maybe some of you have no idea what I’m talking about – but at least I know where to find this damn code again!

 

4 comments about Linking CSS stylesheet on Zend Framework specific pages

  1. Tim Elliott says:

    So I enter “zend css style sheet” into google and guess what I find…
    Cheers mate, still bailing me out on code!
    There’s so many cocking functions, it’s easy to get lost in ZF.

    BTW, happy bday!

    Tim

  2. Daze says:

    Hehe, sweet nice one! Does the job, eh?

    Thanks very much! Haven’t heard from ya in a while, hope things are all well buddy!

    Daze.

  3. [...] David Cooper posted a quick howto on adding CSS to a Zend Framework page, which works and is all well and good, but there was a problem. The code that Zend Framework spits [...]

  4. POC says:

    Do you know a why to set the media attribute via the zend API? I need to be able to define a print.css and ensure the media=”print” attribute is set. So that the final html looks like :

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>