Download the PHP package webrouse/n-asset-macro without Composer
On this page you can find all versions of the php package webrouse/n-asset-macro. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download webrouse/n-asset-macro
More information about webrouse/n-asset-macro
Files in webrouse/n-asset-macro
Package n-asset-macro
Short Description Asset macro for Latte and Nette Framework useful for assets cache busting (with gulp, webpack, etc.)
License MIT
Informations about the package n-asset-macro
Webrouse/n-asset-macro
Asset macro for Latte and Nette Framework.
Useful for assets cache busting with gulp, webpack and other similar tools.
Requirements
Nette 3
is fully supported and tested.
Installation
The best way to install webrouse/n-asset-macro is using Composer:
Then register the extension in the config file:
Usage
Macro can by used in any presenter or control template:
It prepends path with $basePath
or $baseUrl
(see revision manifest:
See the webpack.
Revision manifest
Revision manifest is a JSON file that contains the revision (path or version) of asset.
It can be generated by various asset processors such as gulp and webpack, see examples.
Revision manifest is searched in the asset directory and in the parent directories up to %wwwDir%
.
Expected file names: assets.json
, busters.json
, versions.json
, manifest.json
, rev-manifest.json
.
The path to revision manifest can be set directly (instead of autodetection):
Or you can specify asset => revision
pairs in config file:
Revision manifest may contains asset version or the asset path. Both ways are supported.
Method 1: asset version in file name (preferable)
With this method, the files have a different name at each change.
Example revision manifest:
With the example manifest, the expr. {asset "js/app.js"}
generates: /base/path/js/app.234a81ab33.js
.
Method 2: asset version as a query string
This approach looks better at first glance. The asset path is still the same, and only the parameter in the query changes.
However, it can cause problems with some cache servers, which don't take the URL parameters into account.
Example revision manifest:
With the example manifest, the expr. {asset "js/app.js"}
generates: /base/path/js/app.js?v=234a81ab33
.
Asset macro automatically detects which of these two formats of revision manifest is used.
Macro arguments
format
The format is defined by the second macro parameter or using the format
key (default %url%
).
format
can be used with needed => false
to hide whole asset expression (eg. <link ...
) in case of an error.
You can also use it to include asset content instead of a path.
Placeholder | Example output |
---|---|
%content% |
<svg>....</svg> (file content) |
%path% |
js/main.js or js/main.8c48f58df.js |
%raw% |
8c48f58df or js/main.8c48f58df.js |
%base% |
%baseUrl% if absolute => true else %basePath% |
%basePath% |
/base/path |
%baseUrl% |
http://www.example.com/base/path |
%url% |
%base%%path% (default format) eg. /base/path/js/main.8c48f58df.js |
needed
Error handling is set in the configuration using: missingAsset
, missingManifest
and missingRevision
keys.
These settings can by overrided by third macro parameter or using needed
key (default true
).
Argument needed => false
will cause the missing file or the missing revision record will be ignored.
Missing version will be replaced with unknown
string.
Example of needed
parameter
absent.js
file doesn't exist.missing_rev.js
exists but doesn't have revision in manifest (or the manifest has not been found).
Generated output:
absolute
Output URL type - relative or absolute - is defined by fourth macro parameter or using absolute
key (default false
).
If absolute => true
or asset path is prefixed with //
eg. (//assets/js/main.js
), the absolute URL will be generated instead of a relative URL.
Generated output:
Caching
In production mode is the macro output cached in default application's cache storage.
It can be changed in the configuration using the boolean cache
key.
Configuration
Default configuration, which usually doesn't need to be changed:
ManifestService
It is also possible to access the manifest from your code using Webrouse\AssetMacro\ManifestService
(from DI container).
Examples
Examples based on nette/sandbox:
License
N-asset-macro is under the MIT license. See the LICENSE file for details.
All versions of n-asset-macro with dependencies
latte/latte Version >=2.4
nette/di Version >=2.4
nette/utils Version >=2.4