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)
thanks
it looks very usefull was missing this from cakephp :) will give it a try
Give a Demo link please
It would be nice to look it en action ;-)
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?
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.