m1/twig-asset-rev-extension

Twig Asset Rev Extension adds a asset_rev twig filter so you can use asset revisioning from files, perfect for use with gulp-rev or grunt-rev

1.1.1 2017-01-09 10:17 UTC

This package is auto-updated.

Last update: 2024-10-21 20:51:49 UTC


README

Author Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

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

Requirements

TwigAssetRevExtension requires PHP version 5.3+.

Install

Via Composer

$ composer require m1/twig-asset-rev-extension

Usage

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",
}

Symfony usage

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 }

Setup

new TwigAssetRevExtension(mixed $assets [, bool $minified = true ] )

Parameters

assets

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.

minified

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.

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email hello@milescroxford.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.