Asset クラス

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.

add_path($path, $type = null)

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
パラメータ
パラメータ 規定値 説明
$path 必須 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.
返り値 current instance
/*
 * These paths would need to have subfolders as defined in the config
 * file, depending on the type of content you were trying to use.
 */
Asset::add_path('resources/template_1/');
Asset::add_path('resources/template_2/');

/*
 * Add a global path for all asset types, then add individual paths
 * for images and css files.
 */
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');

/*
 * Create a new asset type
 */
Asset::add_path('assets/docs/', 'pdf');

/*
 * You can chain the calls as well.
 */
Asset::add_path('resources/templates/dark/')
	->add_path('resources/templates/light')
	->add_path('resources/templates/brown');

css($stylesheets = array(), $attr = array(), $group = NULL, $raw = false)

The css method adds css to a named group, the default group, or returns the css tag.

Static Yes
パラメータ
パラメータ 規定値 説明
$stylesheets 必須 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.
返り値 Rendered asset string or current instance when adding to group.
// 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" />
 * if auto_render in the config is set to true.
 * if not, the asset will be added to the default group for later rendering
 */
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, $type, $folder = '')

The find_file locates a file of a given type in all defined asset search folders for that type.

Static Yes
パラメータ
パラメータ 規定値 説明
$file 必須 The name of the file you are searching for.
$type 必須 The type of asset being searched (css, js, img).
$folder
''
The name of the sub-folder to append to each search folder.
返り値 Either the path to the file or false if not found
// find a css file
$path = Asset::find_file('layout.css', 'css');

// find an icon image (assuming icons are in an img sub-folder called 'icons')
$path = Asset::find_file('icon.png', 'img','icons/');

img($images = array(), $attr = array(), $group = NULL)

The img method either adds the image to a named group, the default group, or returns the image tag.

Static Yes
パラメータ
パラメータ 規定値 説明
$images 必須 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.
返り値 Rendered asset string or current instance when adding to group.
/* returns
 * <img src="../assets/img/logo.png" id="logo">
 * if auto_render in the config is set to true.
 * if not, the asset will be added to the default group for later rendering
 */
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 method either adds the javascript to a named group, the default group, or returns the script tag.

Static Yes
パラメータ
パラメータ 規定値 説明
$scripts 必須 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.
返り値 Rendered asset string or current instance when adding to group.
// 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::js('menu_init.js', array(), null, true);

/* returns
 * <script type="text/javascript" src="../assets/js/jquery.js"></script>
 * if auto_render in the config is set to true.
 * if not, the asset will be added to the default group for later rendering
 */
echo Asset::js('jquery.js');

get_file($file, $type, $folder = '')

The get_file method allows you to get the URL to an asset file.

Static Yes
パラメータ
パラメータ 規定値 説明
$file 必須 Name of the asset to look for.
$type 必須 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.
返り値 Fully qualified asset URL (depending on the base_url defined) or false if not found.
// returns something like 'http://example.org/assets/js/jquery.js'
echo Asset::get_file('jquery.js');

remove_path($path, $type = null)

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
パラメータ
パラメータ 規定値 説明
$path 必須 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.
返り値 The current instance
/*
 * Remove global search paths
 */
Asset::remove_path('resources/template_1/');
Asset::remove_path('resources/template_2/');

/*
 * Remove individual paths for different asset types
 */
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');

/*
 * Or chain the calls.
 */
Asset::remove_path('resources/templates/dark/')
	->remove_path('resources/templates/light')
	->remove_path('resources/templates/brown');

render($group = null, $raw = false)

The render renders the group of assets and returns the tags. If no group is specified, the default group will be rendered.

Static Yes
パラメータ
パラメータ 規定値 説明
$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.
返り値 string
/* returns
 * <link href="../assets/css/header.css" rel="stylesheet" />
 * <link href="../assets/css/footer.css" rel="stylesheet" />
 */
echo Asset::render('layout');

// renders the default group
echo Asset::render();