Minify HTML in CodeIgniter using Hooks
Are you trying to minify HTML in Codeigniter? Then this article is for you.
Before starting, lets discuss briefly on Minification.
CodeIgniter
CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications. CodeIgniter is loosely based on the popular model–view–controller (MVC) development pattern.
HTML Minification
HTML Minification is the removal of unnecessary characters and lines in HTML code. Indentation, comments, empty lines, etc. are not required while rendering in HTML. Trimming these details can save download file size not only without affecting the output but also improving the performance.
Sample HTML without Minification
<html> <head> <title>Your Title Here</title> </head> <body> <center> <img src="clouds.jpg" ALIGN="BOTTOM"> </center> <hr> <a href="http://somegreatsite.com">Link Name</a> is a link to another nifty site <h1>This is a Header</h1> <h2>This is a Medium Header</h2> Send me mail at <a href="mailto:support@yourcompany.com">support@yourcompany.com</a> <p> This is a new paragraph! <p> <b>This is a new paragraph!</b> <br> <b><i>This is a new sentence without a paragraph break, in bold italics.</i></b> </p> </p> </body> </html>
HTML after Minification
<html><head><title>Your Title Here</title></head><body><center><img SRC="clouds.jpg" ALIGN="BOTTOM"></center><hr><a href="http://somegreatsite.com">Link Name</a> is a link to another nifty site<h1>This is a Header</h1><h2>This is a Medium Header</h2>Send me mail at <a href="mailto:support@yourcompany.com">support@yourcompany.com</a><p> This is a new paragraph!<p><b>This is a new paragraph!</b><br><b><i>This is a new sentence without a paragraph break, in bold italics.</i></b></p></p></body></html>
Steps to Minify HTML in Codeigniter
Step 1 : Enable Hooks in Config
Go to application/config/config.php and make sure there exists the line
$config['enable_hooks'] = TRUE;
Step 2 : Declare a Hook in hooks.php
Go to application/config/hooks.php and declare a hook like this:
// compress output $hook['display_override'][] = array( 'class' => '', 'function' => 'compress', 'filename' => 'compress.php', 'filepath' => 'hooks' );
Step 3 : Add the hook
Create a file application/hooks/compress.php with the following code in it:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); function compress() { $CI =& get_instance(); $buffer = $CI->output->get_output(); $search = array( '/\n/', // replace end of line by a space '/\>[^\S ]+/s', // strip whitespaces after tags, except space '/[^\S ]+\</s', // strip whitespaces before tags, except space '/(\s)+/s' // shorten multiple whitespace sequences ); $replace = array( ' ', '>', '<', '\\1' ); $buffer = preg_replace($search, $replace, $buffer); $CI->output->set_output($buffer); $CI->output->_display(); } /* End of file compress.php */ /* Location: ./system/application/hooks/compress.php */
That’s it ! Now the HTML rendered with be minified in our webpage.
You may also like: Page Caching to Improve Website Performance
Hi Ritesh, Good post ! Code is working fine.
Thankyou.
Not working on cache file 🙁
I’m looking for some ways to solve xss problem of CI . I think it’s better to solve it when server outputs page than when server inputs data. But the CI obviously thinks not. So I want solve it in my way by using the hooks. I saw this page by accident when I was searching the CI hooks. Although I didn’t find a way to solve the XSS, I got a way to minify my page that I‘ve been looking for for a long time. This may be another kind of luck. Thank you so much !!!