Laravel Elixir twig extension

v1.0.0 2016-12-06 11:38 UTC

This package is not auto-updated.

Last update: 2020-12-04 01:33:14 UTC


Build Status

The Laravel Elixir version task appends a unique hash to filename, allowing for cache-busting.

elixir(function(mix) {

For example, the generated file name will look something like: all-16d570a7.css.

In Laravel, you can use in your views the elixir() function to load the appropriately hashed asset:

<link rel="stylesheet" href="{{ elixir("css/all.css") }}">

This twig extension is an adaptation of this elixir() function.


You need PHP >= 5.5.9 to use the library, but the latest stable version of PHP is recommended.


Install using Composer:

composer require brieucthomas/elixir-twig-extension

This will edit (or create) your composer.json file and automatically choose the most recent version.


Register the extension

use BrieucThomas\Twig\Extension\ElixirExtension;

$elixir = new ElixirExtension(
    $publicDir,     // the absolute public directory 
    $buildDir,      // the elixir build directory (default value is 'build')
    $manifestName   // the manifest filename (default value is 'rev-manifest.json')

Register the extension as a Symfony Service

# app/config/services.yml
        class: BrieucThomas\Twig\Extension\ElixirExtension
        arguments: ["%kernel.root_dir%/../web/"]
        public: false
            - { name: twig.extension }

Create a gulpfile

Here an example of gulpfile.js to compile and version the script app/Resources/js/app.js :

// gulpfile.js
const elixir = require('laravel-elixir');

elixir.config.assetsPath = 'app/Resources';
elixir.config.publicPath = 'web';
elixir.config.appPath = 'src';
elixir.config.viewPath = 'app/Resources/views';

elixir(function(mix) {
    // compile scripts to web/js/all.js (default output)

    // version compiled scripts        

Using the Extension

<link rel="stylesheet" href="{{ elixir('css/all.css') }}">
<script src="{{ elixir('js/all.js') }}"></script>

You can surround with the asset twig extension to make your application more portable:

<link rel="stylesheet" href="{{ asset(elixir('css/all.css')) }}">
<script src="{{ asset(elixir('js/all.js')) }}"></script>