Welcome Guest, Not a member yet? Register   Sign In
Carabiner 1.0: Asset Management Library
#1

[eluser]tonydewan[/eluser]
NOTE: Carabiner has been updated to version 1.42. See the new thread for more info and to download.

Cara-wha?
Carabiner is a library for managing JavaScript and CSS assets. It's different from others that currently exist (for CodeIgniter, anyway) in that it acts differently depending on whether it is in a production or development environment. In a production environment, it will combine, minify, and cache assets. (As files are changed, new cache files will be generated.) In a development environment, it will simply include references to the original assets.


Requirements
Carabiner requires the JSMin and CSSMin libraries that I previously ported. They're both included in this release. You don't need to load them, unless you'll be using them elsewhere. Carabiner will load them automatically as needed. (Note: the only reason they're included as separate libraries is that it allows you to use them independently of Carabiner. If desired, you could probably include them in the carabiner.php file itself. You'd have to edit the Carabiner functions, but it could work.)


GZIP, Packer, etc.
Carabiner does not implement GZIP encoding, because I think that the web server should handle that. I think GZIPing is important, I just prefer not to do it PHP. If you need GZIP in an Asset Library, AssetLibPro does it. I've also chosen not to implement any kind of JavaScript obfuscation (like packer), primarily because of the client-side decompression overhead. More about this idea from John Resig. However, that's not to say you can't do it. You can easily provide a production version of a script that is packed. However, note that combining a packed script with minified scripts could cause problems. In that case, you can flag it to be not combined. (See usage below) Also, that's not to say that I won't port a PHP Version and include it eventually.


Inspiration/License
Carabiner is inspired by Minify, PHP Combine by Niels Leenheer and AssetLibPro by Vincent Esche, among other things. Carabiner is released under a BSD License.


Usage
Load the library as normal:
Code:
$this->load->library('carabiner');


Configure it like so:
Code:
$carabiner_config = array(
    'script_dir' => 'assets/scripts/',
    'style_dir'  => 'assets/styles/',
    'cache_dir'  => 'assets/cache/',
    'base_uri'   => $base,
    'combine'    => TRUE,
    'dev'        => FALSE
);
        
$this->carabiner->config($carabiner_config);


There are 8 relevant configuration options. The first 4 are required for Carabiner to function, the last 4 are not.

script_dir
STRING Path to the script directory. Relative to the CI front controller (index.php)

style_dir
STRING Path to the style directory. Relative to the CI front controller (index.php)

cache_dir
STRING Path to the cache directory. Must be writable. Relative to the CI front controller (index.php)

base_uri
STRING Base uri of the site, like 'http://www.example.com/'


dev
BOOLEAN Flags whether your in a development environment or not. See above for what this means. Defaults to FALSE.

combine
BOOLEAN Flags whether to combine files. Defaults to TRUE.

minify_js
BOOLEAN Flags whether to minify javascript. Defaults to TRUE.

minify_css
BOOLEAN Flags whether to minify CSS. Defaults to TRUE.



Add assets like so:
Code:
$this->carabiner->js('scripts.js');
        
$this->carabiner->css('reset.css');
        
$this->carabiner->css('admin/styles.css');


To set a (prebuilt) production version of an asset:
Code:
// pass a second string to the method with a path to the production version
$this->carabiner->css('wymeditor/wymeditor.js', 'wymeditor/wymeditor.pack.js' );


And to prevent a file from being combined:
Code:
// pass a boolean FALSE as the third attribute of the method
$this->carabiner->css('wymeditor/wymeditor.js', 'wymeditor/wymeditor.pack.js', FALSE );


You can also pass arrays (and arrays of arrays) to these methods. Like so:
Code:
// a single array
$this->carabiner->css( array('base.css', 'base.prod.css') );
        
// an array of arrays
$js_assets = array(
    array('dev/jquery.js', 'prod/jquery.js'),
    array('dev/jquery.ext.js', 'prod/jquery.ext.js'),
)

$this->carabiner->js( $js_assets );


To output your assets, including appropriate markup:
Code:
// display css
$this->carabiner->display('css');
    
//display js
$this->carabiner->display('js');


Finally, since Carabiner won't delete old cached files, you'll need to clear them out manually. To do so programatically:
Code:
// clear css cache
$this->carabiner->empty_cache('css');
        
//clear js cache
$this->carabiner->empty_cache('js');
        
// clear both
$this->carabiner->empty_cache();

Please let me know what you think! If there's something you would like changed or ad


Messages In This Thread
Carabiner 1.0: Asset Management Library - by El Forum - 01-28-2009, 07:23 PM
Carabiner 1.0: Asset Management Library - by El Forum - 01-28-2009, 07:30 PM
Carabiner 1.0: Asset Management Library - by El Forum - 01-28-2009, 09:26 PM
Carabiner 1.0: Asset Management Library - by El Forum - 01-28-2009, 09:56 PM
Carabiner 1.0: Asset Management Library - by El Forum - 01-28-2009, 10:07 PM
Carabiner 1.0: Asset Management Library - by El Forum - 01-28-2009, 10:16 PM
Carabiner 1.0: Asset Management Library - by El Forum - 01-28-2009, 10:24 PM
Carabiner 1.0: Asset Management Library - by El Forum - 01-28-2009, 11:26 PM
Carabiner 1.0: Asset Management Library - by El Forum - 01-29-2009, 12:48 AM
Carabiner 1.0: Asset Management Library - by El Forum - 01-29-2009, 04:58 AM
Carabiner 1.0: Asset Management Library - by El Forum - 01-29-2009, 07:23 AM
Carabiner 1.0: Asset Management Library - by El Forum - 01-30-2009, 05:47 AM
Carabiner 1.0: Asset Management Library - by El Forum - 01-30-2009, 03:22 PM
Carabiner 1.0: Asset Management Library - by El Forum - 02-01-2009, 12:35 AM
Carabiner 1.0: Asset Management Library - by El Forum - 02-01-2009, 04:28 AM
Carabiner 1.0: Asset Management Library - by El Forum - 02-02-2009, 08:44 AM
Carabiner 1.0: Asset Management Library - by El Forum - 02-02-2009, 08:54 AM
Carabiner 1.0: Asset Management Library - by El Forum - 02-06-2009, 12:07 PM
Carabiner 1.0: Asset Management Library - by El Forum - 02-06-2009, 04:00 PM
Carabiner 1.0: Asset Management Library - by El Forum - 02-07-2009, 01:06 AM
Carabiner 1.0: Asset Management Library - by El Forum - 02-09-2009, 01:11 AM
Carabiner 1.0: Asset Management Library - by El Forum - 02-11-2009, 09:12 AM
Carabiner 1.0: Asset Management Library - by El Forum - 02-13-2009, 02:31 PM
Carabiner 1.0: Asset Management Library - by El Forum - 02-13-2009, 03:05 PM
Carabiner 1.0: Asset Management Library - by El Forum - 03-08-2009, 07:40 AM
Carabiner 1.0: Asset Management Library - by El Forum - 03-08-2009, 08:57 AM
Carabiner 1.0: Asset Management Library - by El Forum - 03-08-2009, 09:20 AM
Carabiner 1.0: Asset Management Library - by El Forum - 03-08-2009, 09:46 AM
Carabiner 1.0: Asset Management Library - by El Forum - 03-08-2009, 10:38 AM
Carabiner 1.0: Asset Management Library - by El Forum - 08-25-2009, 04:00 PM
Carabiner 1.0: Asset Management Library - by El Forum - 08-27-2009, 06:39 AM
Carabiner 1.0: Asset Management Library - by El Forum - 08-27-2009, 07:46 AM
Carabiner 1.0: Asset Management Library - by El Forum - 08-26-2010, 01:00 AM
Carabiner 1.0: Asset Management Library - by El Forum - 08-26-2010, 07:58 AM
Carabiner 1.0: Asset Management Library - by El Forum - 08-27-2010, 01:54 AM
Carabiner 1.0: Asset Management Library - by El Forum - 08-27-2010, 02:04 AM
Carabiner 1.0: Asset Management Library - by El Forum - 08-27-2010, 05:33 PM
Carabiner 1.0: Asset Management Library - by El Forum - 08-27-2010, 08:37 PM
Carabiner 1.0: Asset Management Library - by El Forum - 08-28-2010, 09:53 PM
Carabiner 1.0: Asset Management Library - by El Forum - 09-11-2010, 05:35 AM
Carabiner 1.0: Asset Management Library - by El Forum - 08-22-2012, 08:09 AM
Carabiner 1.0: Asset Management Library - by El Forum - 11-09-2012, 07:20 AM



Theme © iAndrew 2016 - Forum software by © MyBB