Yii 1.1: yii-html-compactor

Filter to compress the HTML output
16 followers

The extension is a filter to compress the HTML output - to reduce bandwidth.

The filter offers two compression methods:

  • GZIP compression
  • Trim leading white space and blank lines from HTML output. But does not affect pre, script or textarea blocks.

Heads up: The GZIP compression is turned off for old buggy IE's (< IE7).

Requirements

  • Yii 1.1.10 or above...(could work with older versions)
  • tested with IE7-9, Firefox 13, Chromium, Safari 5.1

Installation

  • Extract the release file under protected/filters
  • put in a controller code blocks like the following...

Usage

See the following code example:

GZIP for all actions

/**
     * @return array action filters
     */
    public function filters()
    {
        return array(
            array(
                'application.filters.html.ECompressHtmlFilter',
                'gzip'            => (YII_DEBUG ? false : true),
                'doStripNewlines' => (YII_DEBUG ? false : true),
                'actions' => '*'
            ),
        );
    }

GZIP only for index & admin action in controller with CRUD actions

/**
     * @return array action filters
     */
    public function filters()
    {
        return array(
            array(
                'application.filters.html.ECompressHtmlFilter',
                'gzip'            => (YII_DEBUG ? false : true),
                'doStripNewlines' => (YII_DEBUG ? false : true),
                'actions' => 'index, admin'
            ),
        );
    }

Trim leading white space, blank lines & new lines from HTML output for all action

/**
     * @return array action filters
     */
    public function filters()
    {
        return array(
            array(
                'application.filters.html.ECompressHtmlFilter',
                'gzip'    => false,
                'actions' => 'ALL'
            ),
        );
    }

Nothing will compressed

/**
     * @return array action filters
     */
    public function filters()
    {
        return array(
            array(
                'application.filters.html.ECompressHtmlFilter',
            ),
        );
    }

Inspiration

Total 5 comments

#18799 report it
moginn at 2015/01/10 09:06am
Low compression level

Apparently the extension compresses well in most cases, but there are some places of my website that this extension causes html errors and the site doesn't display correctly

#18607 report it
jayasam at 2014/11/30 02:16am
Line breaks removed but spaces remained

I enabled

'doStripNewlines' => true,

then line breaks removed but spaces remained.

So I changed 80th line in ECompressHtmlFilter to first compress and then remove line breks. Now it works fine. I don't no why that thing happend.

$content = $this->compressHtml($content);
if ($this->doStripNewlines){
   $content = str_replace(array("\n", "\r"), '', $content);
}
echo $content;
#12792 report it
Trejder at 2013/04/12 04:26am
For all controlleres & actions?

Can this be used in base Controller (descent of CController) class, placed in protected/components, with 'actions' => '*' and thus enable GZIP compression of any output application will generate (any controller and any action)? Or does it have to be put in every controller separately?

#9300 report it
bennouna at 2012/08/02 09:42am
RE: gzip turn on in apache

@Porcelanosa not if you're on a shared webhosting

#9137 report it
Porcelanosa at 2012/07/23 05:05pm
gzip turn on in apache

gzip_deflate almost alwayst turn on in apache - and it gziped all files automatically

Leave a comment

Please to leave your comment.

Create extension