Spiga

Minify css in ZendFramework

by Gabi Solomon

After coming across Matthew Turland Minify Filter for Zend Framework i decided to implement the minify app to also minify the css and javascript external files.

But i only did the implementation for the stylesheets for now, and i wanted to share it :) .

Pre info

Before we go any further i must tell you that this only works if you are using the Zend_View HeadLink Helper to place your css files.

First step i download the minify app and place it in my /public/min folder.

So i already had in my application a lot of calls to the HeadLink Helper and i didnt want to make a new class for this, since that would meant to change all those calls. I wanted a simple method of overwriting the helpers rendering method ( toString ).
To do this i only made my class extend the HeadLink class and wrote a new method for rendering, and also changed the call from the layout file.

[php]
// old call
headLink(); ?>

// new call
minStyleSheets(); ?>
[/php]

The actual class

[php]

class GSD_View_Helper_MinStyleSheets extends Zend_View_Helper_HeadLink {

public function minStyleSheets() {

$items = array();
$stylesheets = array();
foreach ($this as $item) {
if ($item->type == ‘text/css’ && $item->conditionalStylesheet === false) {
$stylesheets[$item->media][] = $item->href;
} else {
$items[] = $this->itemToString($item);
}
}

foreach ($stylesheets as $media=>$styles) {
$item = new stdClass();
$item->rel = ‘stylesheet’;
$item->type = ‘text/css’;
$item->href = $this->getMinUrl() . ‘?f=’ . implode(‘,’, $styles);
$item->media = $media;
$item->conditionalStylesheet = false;
$items[] = $this->itemToString($item);
}

return $indent . implode($this->_escape($this->getSeparator()) . $indent, $items);
}

public function getMinUrl() {
return $this->getBaseUrl() . ‘/public/min/’;
}

public function getBaseUrl() {
return GSD_View_Helper_GetUrl::getBaseUrl();
}

}
[/php]

Info
I have another helper to get the base Url, but you can replace that to what ever you are using to keep the base Url of your application.

That is it folks.
Cheers

  • Lums

    Great tool ;-)
    How can i fix :
    “These components do not have a far future Expires or cache-control: max-age header:” Yslow says that !

  • http://www.gsdesign.ro/ Gabi Solomon

    @LUMS
    thanks … you should modify the min Library to add expires and cache control headers

  • Lums

    Will be great GSD_View_Helper_MinJavaScript….

  • http://www.gsdesign.ro/ Gabi Solomon

    i am working on it

  • http://code.google.com/p/web-optimizator/ sunnybear

    There a lot of issues in CSS merging with different @media / @import / background images / etc. All of them are resolved in complete clientside optimization solution – Web Optimizer ( http://code.google.com/p/web-optimizator/ ). It has been successfully tested for compatibility with Zend Framework.

  • http://n0nick.net/id/ n0nick

    Hey,

    Thanks for the example code – works perfect!
    I have used the main idea to write a basic view helper for Javascript minification: http://pastebin.ca/1702008

  • Bubba

    I appreciated your post, I ran across the same problem and ended up writing two drop-in helpers to manage it for me. You can see them at http://blog.hines57.com/2011/03/13/zendframework-minify/ – thanks again.