Asset クラス

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

Advanced usage

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
パラメータ
パラメータ 規定値 説明
$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.
返り値 object - The instantiated asset object.
// instantiate a asset object with a custom search path
$asset = Asset::forge('custom', array('paths' => '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. If this instance does not exist, it will be forged using the configuration array passed.

Static Yes
パラメータ
パラメータ 規定値 説明
$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.
返り値 mixed - The named asset instance or false if the instance does not exist.
// instantiate a named asset object with a custom search path
Asset::forge('custom', array('paths' => '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!