Yii 1.1: contentcompactor

Compacts HTML, CSS and JS in output
8 followers

Content Compacter removes unnecessary whitespace, intendents and rowbreaks in your rendered viewfiles (HTML). It also compresses inlined CSS and JS in your HTML using borrowed code from minify. It can also remove HTML conditionals specific to IE when detected browser isn't IE.

Resources

Documentation

Requirements

  • Yii 1.1.2 or above
  • Earlier versions should function properly aswell but haven't been tested

Installation

  • Extract and move the folder contentCompactor to protected/extensions
  • Add component to config file

Available options

For more detailed information about options, refer to the authors documentation.

$options = array(
    'compress_css' => true, // Compress CSS
    'strip_comments' => true, // Remove comments
    'keep_conditional_comments' => true, // Remove conditional comments
    'compress_horizontal' => true, // Compress horizontally
    'compress_vertical' => true, // Compress vertically
    'compress_scripts' => true, // Compress inline scripts using basic algorithm
    'line_break' => PHP_EOL, // The type of rowbreak you use in your document
    'preserved_tags' => array('textarea', 'pre', 'script', 'style', 'code'),
    'preserved_boundry' => '@@PRESERVEDTAG@@',
    'conditional_boundries' => array('@@IECOND-OPEN@@', '@@IECOND-CLOSE@@'),
    'script_compression_callback' => false,
    'script_compression_callback_args' => array(),
);

Note: I have stripped out some code from the original Compactor class to minimize server impact since i found some methods unnecessary for Yii usage.

Usage

Add the component to your config file within the components section:

'contentCompactor' => array(
    'class' => 'ext.contentCompactor.ContentCompactor',
    'options' => array(
        '',
    ),
),

If you wish to replace render() within your whole project to use Content Compactors render() then paste the following code into your basecontroller, or in newer versions Controller.php in your components folder:

public function render($view, $data = null, $return = false, $options = null)
{
    $output = parent::render($view, $data, true);
 
    $compactor = Yii::app()->contentCompactor;
    if($compactor == null)
        throw new CHttpException(500, Yii::t('messages', 'Missing component ContentCompactor in configuration.'));
 
    $output = $compactor->compact($output, $options);
 
    if($return)
        return $output;
    else
        echo $output;
}

If you wish to use Content Compacter on specific views using renderCompact(), then use the following code instead:

public function renderCompact($view, $data = null, $return = false, $options=null) 
{
    $output = $this->render($view, $data, true);
 
    $compactor = Yii::app()->contentCompactor;
    if($compactor == null)
        throw new CHttpException(500, Yii::t('messages', 'Missing component ContentCompactor in configuration.'));
 
    $output = $compactor->compact($output, $options);
 
    if($return)
        return $output;
    else
        echo $output;
}

Example usage

/* Common usage */
public function actionRender()
{
    $foo = 'bar';
 
    $this->renderCompact('render', array(
        'Foo' => $foo,
    ));
}
 
/* Return output instead of echo to user */
public function actionRenderReturn()
{
    $output = $this->renderCompact('renderReturn', null, true);
 
    // Do something with $output
    ...
}
 
/* Render with specific options for this call */
public function actionRenderOptions()
{
    $options = array(
        'strip_comments' => true,
        'line_break' => PHP_EOL,
        ...
    );
 
    $this->renderCompact('renderOptions', null, false, $options);
}

Change Log

May 16, 2010

  • Initial release (1.0)

Total 3 comments

#8066 report it
mahan at 2012/05/08 05:16am
got a strange error

Call to undefined method Compactor::init() in D:\xampp\htdocs\xxx\framework\base\CModule.php on line 388

I just followed what the instruction says here.. what is the problem?

#2875 report it
choiceerp at 2011/02/20 11:02am
Give a Demo link please

It would be nice to look it en action ;-)

#388 report it
mech7 at 2010/06/18 08:47am
thanks

it looks very usefull was missing this from cakephp :) will give it a try

Leave a comment

Please to leave your comment.

Create extension
  • Yii Version: 1.1
  • License: New BSD License
  • Developed by: sAe
  • Category: Others
  • Votes: +9
  • Downloaded: 848 times
  • Created on: May 16, 2010
  • Last updated: May 17, 2010