This will output this in Development:
``` html
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">window.asset_merger = true;</script>
<script type="text/javascript" src="/assets/js/functions.js?1320415817"></script>
<link type="text/css" href="/assets/css/site/homepage.css.less?1320504157" rel="stylesheet" />
<link type="text/css" href="/assets/css/main.css?1320508620" rel="stylesheet" />
<link type="text/css" href="/assets/css/notifications.css?1320227001" rel="stylesheet" />
<!--[IF gte IE 7]><link type="text/css" href="/assets/css/ie.css?1320227001" rel="stylesheet" /><![endif]-->
And this in Production:, (*6)
``` html
, (*7)
Virtual folders
---------------
Asset merger combines all your asset files and puts them in a single folder in a publicly accessible directory. This way you can have your assets wherever you want, even outside the document root. When you use the js / css methods of the Asset class it searches the directories you've configured and caches the files to your web folder - both the merged and the individual files.
Remote Files
------------
Asset merger does not do anything to remote files (starting with http://) just adds an html link/script tag to that resource.
IE Conditional Comments
-----------------------
It's a common practice to have conditional comments for CSS / JS files specifically for IE to fight some of it shortcomings. This is supported by asset merger. Assets class methods support a 'condition' option which will wrap the link / script tag in a IE conditional comment.
``` php
<?php echo Assets::factory('main')
->css('site/homepage.css.less')
->css('ie.css.less', array('condition' => 'gte IE 7'))
->css('notifications.css')
->js_block("window.asset_merger = true;", array('condition' => 'gte IE 7'))
->js("functions.js", "jsmin")
?>
JS Local Fallback
Sometimes you need to have a local fallback to your external javascript file (from google js apis for example). You can already achieve this manually with adding a js_block, but there is a cleaner way of doing this. You just set the "fallback" option and asset-merger will do this for you. For example:, (*8)
The first element of the fallback array is the check if jquery is loaded (window.jQuery). The second is the local path to the replacement file. This will generate:, (*10)
merge:
Define the environments that will have a merged version of the assets. This can either be an array or a single environment constant., (*12)
folder:
The URL to the folder that will contain the assets. Will be automatically generated if it's not present in the file system. Must be inside DOCROOT., (*13)
load_paths:
Where to search for files. The CSS and JS files have different directories. Each can be an array of directories., (*14)
processor:
The default processor to be used on each type. This can be overridden for any individual file., (*15)
Engines
The assets class does some processing of the files based on the filename extension. For example if the file ends with .less it will be put through the LESSPHP processor, And if it ends with PHP - through raw PHP. You can also chain Engines so, (*16)
main.css.less.php
Will first pass it through PHP then through LESSPHP, (*17)
The Assets class exposes methods to add assets to it's queue which it then renderer when you convert it to a string (with echo for example), (*21)
function css($file, $processor = null)
function js($file, $processor = null)
Add an asset file to the queue. Thy will be outputted in the order that's given. Or if the files are merged, will appear in the merged files in that order. The second parameter overrides the default processor. You can pass FALSE to disable processing, (*22)
function css_block($content, $processor = null)
function js_block($content, $processor = null)
Those methods place arbitrary content inside the queue to be rendered. This is useful when you want javascript/css to appear in an exact place in your assets loading., (*23)
function merge(bool $merge)
Force merging of the files - useful for testing, (*24)
function process(bool $process)
Force processing of the files - useful for testing, (*25)
function render()
Render the whole queue, this is called automatically on __toString, (*26)
Extending
You can Add your own engines and processors easily by adding a class inside classes/asset/engine, or classes/asset/processor respectfully. The class must have a static method process which will return the desired result., (*27)
Minion Task
If you have a build process that requires you to prebuild the views for your production server you can do that with the included kohana-minion task, (*28)
./minion asset:generate --view={view}
Where --view is the view where your assets are rendered. This way the render/merge code can be executed and all the assets generated from the command line. If you have a seperate merging strategies for each environment you can use environment variables of the command line to set the appropriate Kohana environment (this is accually useful in general)., (*29)