Asset Class
The asset class is a set of methods to help with the collection, grouping and displaying of assets (js, css, img). Each asset is a RELATIVE path from the base_url WITH a trailing slash.
add_path($path)
The add_path method adds the given path to the front of the assets path array.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$path |
required |
The path to add to the front of the assets path array (RELATIVE to base url WITH trailing slash). |
|
Returns |
void |
Example |
Asset::add_path('resources/template_1/');
Asset::add_path('resources/template_2/');
/*
NOTE: The paths referenced above would need to have subfolders of js, css, and/or img
depending on the type of content you were trying to use.
*/
|
css($stylesheets = array(), $attr = array(), $group = NULL, $raw = false)
The css method adds css to a 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. |
$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 file contents directly instead of via a link tag. |
|
Returns |
string |
Example |
// adds file to group and returns '' if group is not null
Asset::css(array('header.css', 'footer.css'), array(), 'layout', false);
/* returns
<link href="../assets/css/inline.css" rel="stylesheet" />
*/
echo Asset::css('inline.css');
/* returns
<style>
.bold_class { font-weight: bold }
#header {height: 50px}
</style>
*/
echo Asset::css('inline.css', array(), null, true);
|
find_file($file, $folder)
The find_file locates a file in all the asset paths.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$file |
required |
The name of the file you are searching for. |
$folder |
required |
The name of the folder which contains the file you are looking for (css, js, img). |
|
Returns |
Either the path to the file or false if not found |
Example |
$path = Asset::find_file('layout.css', 'css');
|
img($images = array(), $attr = array(), $group = NULL)
The img either adds the image to the 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 |
string |
Example |
// returns <img src="../assets/img/logo.png" id="logo">
echo Asset::img('logo.png', array('id' => 'logo'));
Asset::img(array('bob.jpg', 'joe.jpg', 'sally.jpg'), array('class' => 'thumbnail'), 'team_avatars');
|
js($scripts = array(), $attr = array(), $group = NULL, $raw = false)
The js either adds the javascript to the group, or returns the script tag.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$scripts |
required |
An array/string of stylesheet filename(s) to be added to the group or be returned as tags. |
$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 js tags will include the file contents directly instead of via a script src tag. |
|
Returns |
string |
Example |
// returns '' if $raw is set to false
Asset::js(array('jquery.js', 'jqueryui.js'), array(), 'jquery', false);
/* returns
<script type="text/javascript">
var menu = getElementById('menu');
menu.initialize_menu();
</script>
*/
echo Asset::css('menu_init.js', array(), null, true);
// returns <script type="text/javascript" src="../assets/js/jquery.js"></script>
echo Asset::js('jquery.js');
|
remove_path($path)
The remove_path removes the given path from the asset paths array
Static |
Yes |
Parameters |
Param |
Default |
Description |
$path |
required |
The path to from the assets path array. |
|
Returns |
void |
Example |
Asset::remove_path('resources/template_1/');
Asset::remove_path('resources/template_2/');
|
render($group, $raw = false)
The render renders the group of assets and returns the tags.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$group |
required |
The name of the group to render. |
$raw |
false
|
If true this method will return an internal css/js tags. |
|
Returns |
string |
Example |
/* returns
<link href="../assets/css/header.css" rel="stylesheet" />
<link href="../assets/css/footer.css" rel="stylesheet" />
*/
echo Asset::render('layout');
|