Asset Class
The asset class is a set of methods to help with the collection, grouping and displaying of assets (js, css, img).
Using assets can be done in 2 ways: through static usage of the Asset class and through asset objects
returned by the Asset::forge() or Asset::instance(). This section covers static usage which will always work
with the default instance using the configuration specified in the configuration.
Note: the css, js and img methods will return the current instance when adding to a group.
This will happen when you supply a group or auto_render is false and no group (or
null) is supplied in the call.
Using asset objects, Asset::instance() and Asset::forge() is explained in the advanced section.
The add_path method adds the given path to the front of the global assets search path array.
If a $type is specified, it will add the path to the front of the search folder array for the given type.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$path |
required |
The path to add to the front of the assets path array (RELATIVE to the asset url and WITH trailing slash). |
$type |
null
|
If adding a folder, the type to add it to. The asset class has the types 'img', 'css' and 'js' predefined.
If you pass an undefined type, a new paths structure will be created for this type.
To add the same path to multiple types, pass them as an array of types.
|
|
Returns |
current instance |
Example |
Asset::add_path('resources/template_1/');
Asset::add_path('resources/template_2/');
Asset::add_path('assets/global/', array('css', 'js', 'img'));
Asset::add_path('assets/icons/', 'img');
Asset::add_path('assets/images/', 'img');
Asset::add_path('assets/css/', 'css');
Asset::add_path('assets/docs/', 'pdf');
Asset::add_path('resources/templates/dark/')
->add_path('resources/templates/light')
->add_path('resources/templates/brown');
|
The css method adds css to a named group, the default group, or returns the css tag.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$stylesheets |
required |
An array/string of stylesheet filename(s) to be added to the group or be returned as tags, or a string containing inline CSS. |
$attr |
array()
|
An array of attributes to be applied to the css file(s). |
$group |
null
|
A group name to categorize the css. If left null the method will return the css tag. |
$raw |
false
|
If set to true, the result css tags will include the string or file contents directly in the HTML, instead of via a link tag. |
|
Returns |
Rendered asset string or current instance when adding to group. |
Example |
Asset::css(array('header.css', 'footer.css'), array(), 'layout', false);
echo Asset::css('inline.css');
echo Asset::css('inline.css', array(), null, true);
Asset::css(".bold_class { font-weight: bold }\n#header {height: 50px}", array(), 'inline', true);
echo Asset::render('inline');
echo Asset::css(".bold_class { font-weight: bold }\n#header {height: 50px}", array(), null, true);
|
The find_file locates a file of a given type in all defined asset search folders for that type.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$file |
required |
The name of the file you are searching for. |
$type |
required |
The type of asset being searched (css, js, img). |
$folder |
''
|
The name of the sub-folder to append to each search folder. |
|
Returns |
Either the path to the file or false if not found |
Example |
$path = Asset::find_file('layout.css', 'css');
$path = Asset::find_file('icon.png', 'img','icons/');
|
The img method either adds the image to a named group, the default group, or returns the image tag.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$images |
required |
An array/string of image filename(s) to be applied to the group or returned as tags. |
$attr |
array()
|
An array of attributes to apply to the image tag(s). |
$group |
null
|
The group to apply the images to. |
|
Returns |
Rendered asset string or current instance when adding to group. |
Example |
echo Asset::img('logo.png', array('id' => 'logo'));
Asset::img(array('bob.jpg', 'joe.jpg', 'sally.jpg'), array('class' => 'thumbnail'), 'team_avatars');
|
The js method either adds the javascript to a named group, the default group, or returns the script tag.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$scripts |
required |
An array/string of javascript filename(s) to be added to the group or be returned as tags, or a string containing inline javascript. |
$attr |
array()
|
An array of attributes to be applied to the js file(s). |
$group |
null
|
A group name to categorize the js. If left null the method will return the js tag. |
$raw |
false
|
If set to true, the result javascript tags will include the string or file contents directly in the HTML, instead of via a script tag. |
|
Returns |
Rendered asset string or current instance when adding to group. |
Example |
Asset::js(array('jquery.js', 'jqueryui.js'), array(), 'jquery', false);
echo Asset::js('menu_init.js', array(), null, true);
echo Asset::js('jquery.js');
Asset::js("var menu = getElementById('menu');\nmenu.initialize_menu();", array(), 'inline', true);
echo Asset::render('inline');
echo Asset::js("var menu = getElementById('menu');\nmenu.initialize_menu();", array(), null, true);
|
The get_file method allows you to get the URL to an asset file.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$file |
required |
Name of the asset to look for. |
$type |
required |
Type of asset to search for. 'img', 'css', and 'js' are supported types. |
$folder |
''
|
Optionally you can specify sub-folders if the asset is in a sub-folder of one of the defined asset search paths. |
|
Returns |
Fully qualified asset URL (depending on the base_url defined) or false if not found. |
Example |
echo Asset::get_file('jquery.js', 'js');
|
The remove_path method removes the given path from the global assets search path array.
If a $type is specified, it will remove the path from the search folder array for the given type.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$path |
required |
The path to from the assets path array. |
$type |
null
|
If adding a folder, the type to add it to. Currently, 'img', 'css' and 'js' are suppored.
To add the same path to multiple types, pass them as an array of types.
|
|
Returns |
The current instance |
Example |
Asset::remove_path('resources/template_1/');
Asset::remove_path('resources/template_2/');
Asset::remove_path('assets/global/', array('css', 'js', 'img'));
Asset::remove_path('assets/icons/', 'img');
Asset::remove_path('assets/images/', 'img');
Asset::remove_path('assets/css/', 'css');
Asset::remove_path('resources/templates/dark/')
->remove_path('resources/templates/light')
->remove_path('resources/templates/brown');
|
The render renders the group of assets and returns the tags.
If no group is specified, the default group will be rendered.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$group |
null
|
The name of the group to render or null for the default group. |
$raw |
false
|
If true this method will include the contents of css/js files for embedding. |
|
Returns |
string |
Example |
echo Asset::render('layout');
echo Asset::render();
|