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:
  1. // old call
  2.     <?php echo $this->headLink(); ?>
  3.  
  4.     // new call
  5.     <?php echo $this->minStyleSheets(); ?>

The actual class

PHP:
  1. <?php
  2.  
  3. class GSD_View_Helper_MinStyleSheets extends Zend_View_Helper_HeadLink {
  4.    
  5.     public function minStyleSheets() {
  6.  
  7.         $items = array();
  8.         $stylesheets = array();
  9.         foreach ($this as $item) {
  10.             if ($item->type == 'text/css' && $item->conditionalStylesheet === false) {
  11.                 $stylesheets[$item->media][] = $item->href;
  12.             } else {
  13.                 $items[] = $this->itemToString($item);
  14.             }
  15.         }
  16.  
  17.         foreach ($stylesheets as $media=>$styles) {
  18.             $item = new stdClass();
  19.             $item->rel = 'stylesheet';
  20.             $item->type = 'text/css';
  21.             $item->href = $this->getMinUrl() . '?f=' . implode(',', $styles);
  22.             $item->media = $media;
  23.             $item->conditionalStylesheet = false;
  24.             $items[] = $this->itemToString($item);
  25.         }
  26.  
  27.         return $indent . implode($this->_escape($this->getSeparator()) . $indent, $items);
  28.     }
  29.  
  30.     public function getMinUrl() {
  31.         return $this->getBaseUrl() . '/public/min/';
  32.     }
  33.  
  34.     public function getBaseUrl() {
  35.         return GSD_View_Helper_GetUrl::getBaseUrl();
  36.     }
  37.  
  38. }

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

Related Posts

  • 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 !
  • @LUMS
    thanks ... you should modify the min Library to add expires and cache control headers
  • Lums
    Will be great GSD_View_Helper_MinJavaScript....
  • i am working on it
  • 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.
  • 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
blog comments powered by Disqus