Geeky Nuggets

CSS minification library for codeigniter

April 11, 2011 | 2 Minute Read

For my latest project, I wanted to be able to easily minify CSS stylesheets, using the simplest syntax possible, ideally using a simple tag in the header, like

<?php echo $this->css->link(array('/assets/css/fonts.css','/assets/css/layout.css');?>

After some searching, I found a css minification library for PHP, minify

It was very easy to modify it so that It could be used in our CodeIgniter applications like any other libraries. All I did was wrap the main minification function in Codeigniter specific code. I modelled the interaction on the image library, as I guess that’s what most people are used to.

css minification library for codeigniter
css minification for codeigniter

How to use

Using the library involves loading it, setting up an array with some configuration values, such as the source css file(s), the destination file (optional) or the time that the generated file should be cached, and finally initializing the library with the configuration array() .

The following code, which should go in your controller, will look very familiar to Codeigniter users.

<?php
$this->load->library('css');
//array of files to concatenate and minify
$config['source_file']=array('/assets/css/layout.css','/assets/css/fonts.css');
//where to output the result
$config['dest_file']'/assets/css/css.min.css';
//Cache time in hours
$config['cache_time']=48;
$this->css->initialize($config);
echo $this->css->link();
?>

Will output the following stylesheet tag:

<link rel="stylesheet" href="/assets/css/css.min.css" type="text/css" media="screen" />

Get It!

You can get it from GitHub. Don’t forget to fork it if you think you can make it better!

Finally

I’m not sure the way I’ve done it, using a

$config

array and so on, is the best way. It might be a bit overkill for such a simple library. I’d love to hear your thoughts on the subject.

Older posts