11# Asset
22
3- The ` Asset ` helper is used to translate asset names.
4- It could be used to prevent browser caching for assets.
3+ The ` Asset ` helper is used to map asset names to versioned assets.
4+
5+ This can be used to allow using a single, canonical name for an asset within
6+ your view scripts, while having that map to:
7+
8+ - A versioned asset name, used to prevent browser caching.
9+ - A product of a build process (such as a CSS pre-processor, JS compiler, etc.)
510
611## Configuration and Basic Usage
712
8- ` Zend\View\Helper\Service\AssetFactory ` checks the application
9- configuration, making it possible to set up the resource map through
10- your ` module.config.php ` . The next example will set up ` Asset ` helper :
13+ ` Zend\View\Helper\Service\AssetFactory ` checks the application configuration,
14+ making it possible to set up the resource map through your ` module.config.php `
15+ or application configuration. As an example:
1116
1217``` php
1318'view_helper_config' => [
@@ -20,37 +25,35 @@ your `module.config.php`. The next example will set up `Asset` helper:
2025],
2126```
2227
23- Then in your view you can use :
28+ Within your view script, you would reference the asset name :
2429
2530``` php
2631// Usable in any of your .phtml files:
2732echo $this->asset('css/style.css');
2833```
2934
30- and you would receive following output:
35+ which then emits the following output:
3136
3237``` html
3338css/style-3a97ff4ee3.css
3439```
3540
36- The first argument of the ` asset ` helper is the regular asset name,
37- which will be replaced by versioned asset name defined in ` resource_map `
38- of the configuration.
41+ The first argument of the ` asset ` helper is the regular asset name, which will
42+ be replaced by the associated value defined in the ` resource_map ` of the
43+ configuration.
3944
40- > ### Note
45+ > ### Exceptions
4146>
42- > When ` asset ` key is defined but ` resource_map ` is not provided or is not
43- > an array exception ` Zend\View\Exception\RuntimeException ` will be
44- thrown.
47+ > When an ` asset ` key is specified but the ` resource_map ` is not provided or is not
48+ > an array, the helper will raise a ` Zend\View\Exception\RuntimeException ` .
4549>
46- > When you call ` asset ` helper with parameter which is not defined on your
47- > ` resource_map ` exception ` Zend\View\Exception\InvalidArgumentException `
48- > will be thrown.
50+ > When you call the ` asset ` helper with a parameter not defined in your
51+ > ` resource_map ` , the helper will raise a ` Zend\View\Exception\InvalidArgumentException ` .
4952
5053## Resource map in JSON file
5154
52- If you have JSON file with resource map, for example
53- ` rev-manifest.json ` :
55+ A number of build tools, such as gulp-rev and grunt-rev, will create a JSON
56+ resource map file such as ` rev-manifest.json ` :
5457
5558``` javascript
5659{
@@ -59,16 +62,17 @@ If you have JSON file with resource map, for example
5962}
6063```
6164
62- then you can have in your configuration:
65+ You can incorporate these into your configuration manually by fetching and
66+ decoding the contents:
6367
6468``` php
6569'view_helper_config' => [
6670 'asset' => [
67- 'resource_map' => json_decode(file_get_contents('/ path/to/rev-manifest.json'), true),
71+ 'resource_map' => json_decode(file_get_contents('path/to/rev-manifest.json'), true),
6872 ],
6973],
7074```
7175
72- and when you have enabled cache config this file will be also cached in
73- compiled configuration cache, so it prevents reading the file on each
74- page load .
76+ If you have enabled configuration caching, these values _ will also be cached _ ,
77+ meaning that the above operation will occur exactly once in your production
78+ configuration .
0 commit comments