Set media=print Using The CakePHP CSS HtmlHelper

It seemed so simple, yet it took forever to figure this one out. I’ve been using CakePHP for several projects lately, and I’m really enjoying using it. But much of the documentation is lacking. Luckily there are plenty of blogs detailing the progress of this framework and what can be done with it. But I searched in vain for quite a while on how to create a link to a css file and designate it for “print” only.

Here’s how to create a link to a stylesheet and set the media type to “print”:

$html->css(array('filename'), 'stylesheet', array('media' => 'print'));

That will output the following code:

Hope that helps the next person trying to figure this one out.

  1. I believe that all of the ‘helpers’ work that way. The final argument is an array of HTML attributes and their values.

    You can use it to assign “onclick” events and other attributes.

  2. @Michael Barrett: You are absolutely right there.

    An even more straightforward portion of code to generate a link tag with media-field would be “$html->css(‘filename’, null, array(‘media’ => ‘print’));” btw.

    • Eugene
    • October 20th, 2008

    Now everyone is talking about the American economy and eclections, nice to read something different. Eugene

    • Sonny
    • November 27th, 2008

    It’s great to see you are using Cake…it’s absolutely amazing how you can put together a complex database driven site in minutes! I’ve been learning how to use Cake too and it’s drastically changed my life!

    • Sharandeep Brar
    • April 1st, 2009

    Thanks, it saved me some time 🙂

  3. Thanks, this worked great for me as well! I was able to apply this to my default view to get the print stylesheet and hide the actions.

  4. Cool! I’m new to CakePHP, and this is exactly what I was looking for… Thank you. 🙂

    • Fábio
    • January 24th, 2011

    Thank you. It’s works for me.

  5. Thank you very much! It works on my project.

  1. No trackbacks yet.