All posts tagged Open Source

Assets – A PHP 5 Asset Combining Class

Assets takes multiple JavaScript or CSS files and combines them into one.

So mootools.js, drag.js, slider.js, and tips.js can become script.php?load=mootools,drag,slider,tips.

Rather then four script tags, you have one. I recently published this Open Source PHP class to combine different text files into one in real time. The benefit here is JavaScript management and page loading speed. Making a bunch of HTTP requests to external JavaScript will slow down the page load, if you don’t believe me just open your website and profile it using the WebKit Inspector.

Documentation

There are two ways to really use Assets.

Automatic

Assets::factory()->get()->render();

This method implies that you are calling the page with a parameter of “load” in the URL. The file names need to be separated by commas. Example: script.php?load=mootools,drag,slider,tips

Manual

Assets:factory()->add('file.js')->add('file2.js')->render();

This methods adds each file individually passed in using PHP. It is worth noting that you can also pass and array of file names into the add() method.

Notes

If you prefer to use the new Assets that is completely OK. You don’t have to use the factory() method. Also, I am open to feedback; in fact that’s half the reason I release my code for free is for people to tear it up and tell me what to improve.

Download

Assets is on GitHub