Twig Asset Rev Extension adds a asset_rev
twig filter so you can use asset revisioning from files, perfect for use
and tested with gulp-rev
/gulp-rev-all
or grunt-rev
TwigAssetRevExtension
requires PHP version 5.3+
.
Via Composer
$ composer require m1/twig-asset-rev-extension
TwigAssetRevExtension
works like other twig extensions, just add the extension using $twig->addExtension()
.
Use \M1\TwigAssetRevExtension\TwigAssetRevExtension;
$assets = json_decode(file_get_contents('rev-manifest.json'), true);
$asset_rev = new TwigAssetRevExtension($assets);
$twig->addExtension($asset_rev);
example.twig
:
<link href='{{"css/app.css"|asset_rev}}' rel='stylesheet'>
rev-manifest.json
:
{
"css/app.css": "css/app.bd6efcb01bc3.css",
"css/app.min.css": "css/app.min.9f8d3d255c1f.css",
}
You can register the extension automatically in Symfony via app/config/services.yml
.
twig.extension.asset_rev:
class: M1\TwigAssetRevExtension\TwigAssetRevExtension
arguments: [ "%kernel.root_dir%/../web/js/manifest.json", false ]
tags:
- { name: twig.extension }
new TwigAssetRevExtension(mixed $assets [, bool $minified = true ] )
The path to the JSON file, or the array of assets and rev'd assets, an example:
array(
"css/app.css" => "css/app.bd6efcb01bc3.css",
"css/app.min.css" => "css/app.min.9f8d3d255c1f.css",
"js/app.admin.js" => "js/app.admin.96b3cc15df52.js",
"js/app.admin.min.js" => "js/app.admin.min.dbdc6d8e2114.js",
"js/app.admin.plugins.js" => "js/app.admin.plugins.927a9b50dd18.js",
"js/app.admin.plugins.min.js" => "js/app.admin.plugins.min.283a1a903f4a.js",
"img/image-jpg.jpg" => "img/image-jpg.219a48cfe072.jpg",
"img/image-png.png" => "img/image-png.1691620d298a.png",
"img/image-gif.gif" => "img/image-gif.bcd9f17c5cf8.png"
)
If using gulp-rev
or gulp-rev-all
this is just the contents of rev-manifest.json
- parsing the json file with
json_decode(file_get_contents('rev-manifest.json'), true);
. You should probably cache this result though so you don't have to
read the file every request.
When true
this means that TwigAssetRevExtension
will pass back minified assets if they're available. Twig debug mode
or Silex debug mode override this and it won't pass back minified assets in development.
Please see CHANGELOG for more information what has changed recently.
$ composer test
Please see CONTRIBUTING and CONDUCT for details.
If you discover any security related issues, please email hello@milescroxford.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.