Minify css in ZendFramework
by Gabi SolomonAfter 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.
The actual class
-
<?php
-
-
class GSD_View_Helper_MinStyleSheets extends Zend_View_Helper_HeadLink {
-
-
public function minStyleSheets() {
-
-
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->media = $media;
-
$item->conditionalStylesheet = false;
-
$items[] = $this->itemToString($item);
-
}
-
-
}
-
-
public function getMinUrl() {
-
return $this->getBaseUrl() . '/public/min/';
-
}
-
-
public function getBaseUrl() {
-
return GSD_View_Helper_GetUrl::getBaseUrl();
-
}
-
-
}
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
-
Gabi Solomon
-
Lums
-
Gabi Solomon
-
sunnybear
-
n0nick

