1
1
# Asset
2
2
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.)
5
10
6
11
## Configuration and Basic Usage
7
12
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:
11
16
12
17
``` php
13
18
'view_helper_config' => [
@@ -20,37 +25,35 @@ your `module.config.php`. The next example will set up `Asset` helper:
20
25
],
21
26
```
22
27
23
- Then in your view you can use :
28
+ Within your view script, you would reference the asset name :
24
29
25
30
``` php
26
31
// Usable in any of your .phtml files:
27
32
echo $this->asset('css/style.css');
28
33
```
29
34
30
- and you would receive following output:
35
+ which then emits the following output:
31
36
32
37
``` html
33
38
css/style-3a97ff4ee3.css
34
39
```
35
40
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.
39
44
40
- > ### Note
45
+ > ### Exceptions
41
46
>
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 ` .
45
49
>
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 ` .
49
52
50
53
## Resource map in JSON file
51
54
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 ` :
54
57
55
58
``` javascript
56
59
{
@@ -59,16 +62,17 @@ If you have JSON file with resource map, for example
59
62
}
60
63
```
61
64
62
- then you can have in your configuration:
65
+ You can incorporate these into your configuration manually by fetching and
66
+ decoding the contents:
63
67
64
68
``` php
65
69
'view_helper_config' => [
66
70
'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),
68
72
],
69
73
],
70
74
```
71
75
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