Asset Class

The asset class is a set of methods to help with the collection, grouping and displaying of assets (js, css, img, or custom types).

Multiple asset instances

The Asset class supports the use of multiple instances. This might prove useful if you are using themes, where every theme has its own set of assets, or you are developing a modular application where each module provides its own set of assets.

In these cases, it would not be handy to have only a single instance and use search paths, since the chances of a duplicate asset name are quite high, causing your application to load the wrong asset.

You can use the same methods on an asset instance as you would when you the static methods. For the method definition see the class usage page.

forge($config = array())

The forge method allows you to manually instantiate an asset instance.

Static Yes
Parameters
Param Default Description
$name
null
Name of the instance requested. If it doesn't exist, it will be forged. If no name is given, the default instance is returned.
$config
array()
You can pass a custom configuration when forging a new asset instance. The configuration array has the same structure as the asset configuration file. The custom config will be merged with a default configuration as documented here and with the configuration in your asset config file, so you only have to pass values you want changed.
Returns object - The instantiated asset object.
Example
// instantiate a asset object with a custom search path
$asset = Asset::forge('custom', array('paths' => array('custom/assets/'));

// define some css files
$asset->css(array('header.css', 'footer.css'), array(), 'layout', false);

instance($name = null)

The instance method allows you load a named instance of the asset class.

Static Yes
Parameters
Param Default Description
$name
null
Name of the instance requested. If it doesn't exist, false is returned. If no name is given, the default instance is returned.
Returns mixed - The named asset instance or false if the instance does not exist.
Example
// instantiate a named asset object with a custom search path
Asset::forge('custom', array('paths' => array('custom/assets/')));

// define some css files using the defined instance
Asset::instance('custom')->css(array('header.css', 'footer.css'), array(), 'layout', false);

The static methods of the Asset class will also use the default instance. Remember this if you alter the configuration (for example search paths) of the default instance!


Adding custom asset types

The asset instance allows you to add custom types, so you can have it render other files or tags than only CSS, JS or IMG tags.

add_type($type, $path = null, $renderer = null)

The add_type method allows to add custom types to be rendered by the Asset class at runtime.

Static No
Parameters
Param Default Description
$type
null
Name of the type you want to be able to render as part of your View output. You could for example define the type 'youtube' to have Asset render the HTML needed to embed a youtube video on your page.
$path
null
If relevant for your type, the path from which the assets have to be loaded. In the example of 'youtube', you won't need a path, as the assets will be loaded from an internet URL.
$renderer
null
A closure that will render your type. The closure will get three parameters passed: the name of the file to render, an array of attributes, and a boolean whether or not there is inline display required. Note that not all of this could be relevant for your type. For example, javascript can be loaded from file or rendered inline between script tags. An image however can only be rendered as an "img" tag, you can not include it inline in the HTML.
Returns string - The rendered HTML.
Example
// create a youtube renderer on the default asset instance
Asset::instance()->add_type(
    'youtube',
    null,
    function ($file, $attr, $inline) {
		// simple example, just renders an iframe
		return '<iframe width="420" height="315" src="http://www.youtube.com/embed/'.$file.'?autoplay=1"></iframe>';
	}
);

// render a youtube movie
echo Asset::instance()->youtube('XGSy3_Czz8k');