Asset クラス
The asset class is a set of methods to help with the collection, grouping and displaying of assets (js, css, img).
設定
The asset class's defaults are configured through the fuel/core/config/asset.php configuration file. It is already populated with a default configuration. You can override this configuration adding a config file with the same name to your application config directory, and set the values you want to change there. These will overwrite the core config but keep what you didn't overwrite.
The following global configuration values can be defined:
パラメータ | 型 | 規定値 | 説明 |
---|---|---|---|
paths | array |
|
The default asset root paths. Paths must include a trailing slash. |
css_dir | string |
|
The default css sub-folder. It must include a trailing slash. |
js_dir | string |
|
The default js sub-folder. It must include a trailing slash. |
img_dir | string |
|
The default image sub-folder. It must include a trailing slash. |
folders | array |
|
To specify one or more individual asset folders per asset type. |
url | string |
|
URL to the root of your assets. When loading an asset, the asset path and the asset name will be appended to this URL. |
add_mtime | bool |
|
Whether to append the assets last modified timestamp to the url. This will aid in asset caching, and is recommended. |
indent_level | integer |
|
To keep the generated HTML tidy, the Asset class can indent tags. This sets the level of indentation. |
indent_with | string |
|
String to indent the generated HTML with. This defaults to a single tab. |
auto_render | bool |
|
This controls whether to return the generated HTML when an asset method is called without a group name. If false, the asset will be added to the default group to be rendered later. |
fail_silently | bool |
|
When set to true the exception will not be thrown when an asset file is not found. The asset will then be ignored. |
Asset paths
There are several ways to define asset paths. You can define global asset search paths, which define the 'root' of your assets. The sub-folder defined for the loaded asset is appended to these paths when searching for an asset. If you use multiple global search paths, they must all use the same sub-folder structure.
// assuming:
// - your URL is "http://example.org/"
// - your global search path is "assets/"
// - your img_dir is "img/"
// this would return <img src="http://example.org/assets/img/icons/myicon.png" />
echo Asset::img('icons/myicon.png');
If you don't want a standard sub-folder structure, or you have to work in an existing environment in which the asset paths are already defined, the Asset class allows you to define the individual asset search folders for each type of asset supported.
// if this is your config
// 'folders' => array(
// 'css' => array('assets/css'),
// 'js' => array('assets/js', 'global/js/'),
// 'img' => array('assets/img/', 'assets/icons'),
// ),
// and your icons are in 'assets/icons',
// this would return <img src="http://example.org/assets/icons/myicon.png" />
echo Asset::img('myicon.png');
Both search paths and search folders can be defined at runtime using the add_path() method.
Static assets
If (some of your) your assets are not stored within the docroot of the application but are using a different host in the URL, you use the following techniques to load an asset:
// individually, per asset
echo Asset::img('http://static.example.org/icons/myicon.png');
// for all sets, set your Asset URL to 'http://static.example.org/'
echo Asset::img('icons/myicon.png');
In this case search paths are ignored.