Download the PHP package adriansuter/twig-cache-busting without Composer
On this page you can find all versions of the php package adriansuter/twig-cache-busting. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download adriansuter/twig-cache-busting
More information about adriansuter/twig-cache-busting
Files in adriansuter/twig-cache-busting
Package twig-cache-busting
Short Description Twig Cache Busting is an add-on for Twig to support cache busting on template compilation.
License MIT
Informations about the package twig-cache-busting
Twig-Cache-Busting
Twig Cache Busting is an add-on for Twig to support cache busting on template compilation.
Installation
Description
This add-on would extend Twig by a cache busting mechanism. The cache busting is taking place upon compilation of the template (not upon rendering). So whenever you update an asset, you would need to recompile the templates that reference this asset (or clear the cache and let Twig rebuild it automatically). The benefit is, that if you cache your compiled templates, then the server would process the performance intense cache busting only once (not on every request).
Cache Busters
By default, there are three cache busting methods. But you can develop your own custom cache buster by implementing
the \AdrianSuter\TwigCacheBusting\Interfaces\CacheBusterInterface
.
- Query Param Cache Buster
This cache buster would append a query param to the file path. So an asset with the path
image.jpg
gets transformed for example toimage.jpg?c=abcd
. - File Name Cache Buster
This cache buster would alter the file name. So an asset with the path
image.jpg
gets transformed for example toimage.abcd.jpg
. - Dictionary Cache Buster This cache buster would use a lookup table (map) to find the target file path.
Hash Generators
The Query Param Cache Buster and the File Name Cache Buster both use a hash generator
to generate a hash for the given asset (in the examples above, the hash is abcd
). By default
the following hash generators are possible. But you can develop your own custom hash generator
by implementing \AdrianSuter\TwigCacheBusting\Interfaces\HashGeneratorInterface
.
- FileMD5HashGenerator This hash generator calculates the MD5 hash of the file.
- FileSHA1HashGenerator This hash generator calculates the SHA1 hash of the file.
- FileModificationTimeHashGenerator This hash generator uses the file modification time as Unix timestamp.
Usage
Query Param Cache Buster
Assume you have a file /home/htdocs/public/assets/image.jpg
and your template is as follows:
To use the Query Param Cache Buster you need to pass the QueryParamCacheBuster
to the static create
method of the CacheBustingTwigExtension
.
By default, the QueryParamCacheBuster
uses the FileModificationTimeHashGenerator
. But
you can set another generator by passing a second argument to the constructor. For
example:
File Name Cache Buster
Assume you have a file /home/htdocs/public/assets/image.jpg
and your template is as follows:
To use the File Name Cache Buster you need to pass the FileNameCacheBuster
to the static create
method of the CacheBustingTwigExtension
.
Your web server needs to be configured such that the cache busting requests get redirected. For Apache you would need to set
Note that you might want to add more extensions to the rewrite rule.
By default, the FileNameCacheBuster
uses the FileModificationTimeHashGenerator
. But you can set another
generator by passing a second argument to the constructor. For example:
If your hash generator returns hexadecimal hashes, then you would need to adapt the Apache rewrite rule appropriately. For example:
Dictionary Cache Buster
This cache busting method would use a dictionary to lookup file names. The dictionary is basically a mapping between the original file path and the cache busting version.
Base Path
If you want to prepend a base path to the generated paths, then simply pass that to the
static create
method of the CacheBustingTwigExtension
. For example:
Custom Twig Tag
If you want to change the twig tag cache_busting
into something else, you can do that simply
by setting the third argument of the static create
method of the CacheBustingTwigExtension
.
For example:
Now you can use cb
in your templates in order to apply cache busting.
Contribution
Is much welcomed.