Set media=print Using The CakePHP CSS HtmlHelper
Posted on July 18th, 2008 in CakePHP, Web Development, Code |
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:
<link href="/css/filename.css" rel="stylesheet" type="text/css" media="print" />
Hope that helps the next person trying to figure this one out.
2 Responses
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.
@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.