agallou/twig-hashed-file-extension

Include file containing content hash in twig templates

0.1.0 2016-03-13 10:33 UTC

This package is auto-updated.

Last update: 2024-03-29 03:13:14 UTC


README

The grunt-hash grunt plugin allows you to rename files according to their content.

The robo-hash task allows you to to the same in PHP.

For example a file named main.css will be renamed to main.54e79f6f.css.

So the file is not easy to include in a Twig template (its name will change at every content change).

This plugin adds a twig function called hashed_file.

Usage

You can call it like this :

$twig->addExtension(new \Agallou\TwigHashedFileExtension\Extension(__DIR__ . '/web/assets/', null));
<link rel="stylesheet" href="{{ hashed_file('css/main.css') }}" />

It will look for files called main*.css in the web/assets/css directory and serve it as /assets/main.54e79f6f.css.

If no file is found or more than one file is found an exception will be thrown.