Gets or sets options for various asset types.
public static assets ( string $type = null, array $options = [] ) : array | ||
$type | string | The name of the asset type, i.e. `'js'` or `'css'`. |
$options | array | If registering a new asset type or modifying an existing asset type, contains settings for the asset type, where the available keys are as follows: - `'suffix'`: The standard suffix for this content type, with leading dot ('.') if applicable. - `'filter'`: An array of key/value pairs representing simple string replacements to be done on a path once it is generated. - `'paths'`: An array of key/value pairs where the keys are `String::insert()` compatible paths, and the values are array lists of keys to be inserted into the path string. |
return | array | If `$type` is empty, an associative array of all registered types and all associated options is returned. If `$type` is a string and `$options` is empty, returns an associative array with the options for `$type`. If `$type` and `$options` are both non-empty, returns `null`. |
public function testCustomAssetPathGeneration() { Media::assets('my', array('suffix' => '.my', 'path' => array('{:base}/my/{:path}' => array('base', 'path')))); $result = Media::asset('subpath/file', 'my'); $expected = '/my/subpath/file.my'; $this->assertEqual($expected, $result); Media::assets('my', array('filter' => array('/my/' => '/your/'))); $result = Media::asset('subpath/file', 'my'); $expected = '/your/subpath/file.my'; $this->assertEqual($expected, $result); $result = Media::asset('subpath/file', 'my', array('base' => '/app/path')); $expected = '/app/path/your/subpath/file.my'; $this->assertEqual($expected, $result); $result = Media::asset('subpath/file', 'my', array('base' => '/app/path/')); $expected = '/app/path//your/subpath/file.my'; $this->assertEqual($expected, $result); }
<?php use lithium\action\Dispatcher; use lithium\net\http\Media; Dispatcher::applyFilter('_callable', function ($self, $params, $chain) { Media::type('default', null, array('theme' => 'default', 'view' => 'li3_themes\\template\\View', 'paths' => array('layout' => '{:library}/webroot/themes/{:theme}/views/layouts/{:layout}.{:type}.php', 'template' => '{:library}/webroot/themes/{:theme}/views/{:controller}/{:template}.{:type}.php', 'element' => '{:library}/webroot/themes/{:theme}/views/elements/{:template}.{:type}.php'), 'webroot' => '{:library}/webroot/themes/{:theme}')); Media::assets('img', array('paths' => array('{:base}/themes/{:theme}/img/{:path}' => array('base', 'theme', 'path')), 'theme' => 'default')); Media::assets('js', array('paths' => array('{:base}/themes/{:theme}/js/{:path}' => array('base', 'theme', 'path')), 'theme' => 'default')); Media::assets('css', array('paths' => array('{:base}/themes/{:theme}/css/{:path}' => array('base', 'theme', 'path')), 'theme' => 'default')); return $chain->next($self, $params, $chain); });