Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 19 revisions

File:Asset_Linker_0.1.1.zip [h3]Introduction[/h3] Asset Linker is a library specifically to help with storage of css/js files and the loading of them.

I don't know about you but when I am writing my websites I find I suddenly need a new css file for just this method of a controller. Now having to add it into the view file manually is just a pain and would mean a new view file for just this method. Also I have a selection of js/css files I need all the time.

So I created this library to try and ease the process. The library can perform the following functions.

  1. Load a set of default assets including ones specifically for the public/admin & shared assets
  2. Load an asset on the fly for a particular controller/method
  3. Compress all public/admin/shared assets into a single file to save the browser loading many different files [h3]Download[/h3] File:Asset_Linker_0.1.1.zip [h3]Usage[/h3] [h4]Load asset on the fly[/h4] To load an asset on the fly takes just one line of code. [code] // $level is ONE of the following public/admin/shared // $type is ONE of the following css/js // $name Name of file with extension $this->assets->load($level,$type,$name); [/code]

[h4]Output assets[/h4] In the view file between the html HEAD tags include this line. [code] // $level is ONE of the following public/admin $this->assets->link($level);[/code]

[h4]Config Settings[/h4] There are several settings used to set-up the library. All are explained in full in the actual config file. In short you can set-up the default assets/asset locations & asset caching settings.

[h4]Asset Caching[/h4] But the browser caches css/js files?? I hear you say. Yes it does but if you say have many javascript and css files it still takes quite a while to load. I have provided an example below.

Say we have 5 css stylesheets & 4 javascript files which we need to load on ever page. The total size of all the files is 180kb uncompressed. We have several possible cases, first-time page load/return visit & asset caching enabled/disabled.

[quote]Page load times: First-time visit + No Asset caching = 6.65s First-time visit + Asset caching = 3.07s Return visit + No Asset caching = 3.61s Return visit + Asset caching = 1.00s[/quote]

So as you can see when asset caching is enabled you get on average 36% speed-up. BUT this is not totally true, you only get this if the same person visits only twice. In real use you will only re-cache the asset files maybe once a day, so if 100 people visited your site twice in a day, only the first visit would get non cached asset files.

Another factor is these calculations have been made using non-compressed cache files.Compressing both js and css files can decrease there size by nearly 50%.

[h3]Notes[/h3] PLEASE NOTE I HAVE TESTED THE LIBRARY BUT CANNOT GUARANTEE IT TO WORK PERFECTLY. THERE ARE ALSO SEVERAL FEATURES I HAVE NOT FULLY IMPLEMENTED (LIKE THE CACHE COMPRESSION FUNCTIONS) I HOPE I CAN FINISH THESE OFF SOON.

Please also take into consideration if you use this library that this is my first real piece of code I have released for public use, so my documentation and coding standards may be lacking. I hope it can be of use to someone. Please do get back to me in the relevant CI forum [url=http://codeigniter.com/forums/viewthread/67740/]thread[/url] about any problems and improvements.

[em][b]Asset Linker library was tested using PHP 4.4.4[/b][/em]

Category:Library Category:Assets

Clone this wiki locally