Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force assetic to render assets each time the page is reloaded?

How can I force assetic to render assets each time the page is reloaded (no matter if assets are modified or not)?

More explanation about my issue:

I'm currently working on a Symfony2 project where I use Assetic to manage and compile .less files. I got everything to work fine but I'm having a small issue that I'd like to fix.

In config.yml, I set the assetic use_controller to true.

# Assetic Configuration
assetic:
debug:          %kernel.debug%
use_controller: true

The result is that Symfony dynamically renders the new .css files each time .less files are modified. This is great.

My problem is that I use a main project.less file where I import all the other .less files

// Import Twitter Bootstrap
@import "../../../../../../vendor/twitter/bootstrap/less/bootstrap.less";

// Import Foo
@import "foo.less";

...

it allows me to keep a clean structure and also to import .less files from vendors, e.g: twitter bootstrap.

In my Twig template, I only call this main file.

{% stylesheets '@ProjectWebBundle/Resources/public/less/project.less' filter='less' %}
        <link rel="stylesheet" type="text/css" media="screen" href="{{ asset_url }}" />
{% endstylesheets %}    

Since this main .less file is never modified, Assetic doesn't recompile the assets. This is why I'd like it to render the files not matter if they've been modified or not.

like image 844
Evarouss Avatar asked Aug 22 '12 14:08

Evarouss


2 Answers

I'm using Assetic's Lessphp filter that caches files too. For myself I've created a class that extends default Assetic's filter and touches every file with current time

<?php

namespace Xxx\AssetsBundle\Assetic\Filter;

use Assetic\Asset\AssetInterface;
use Assetic\Filter\LessphpFilter;

class LessphpNonCachedFilter extends LessphpFilter
{
    public function filterLoad(AssetInterface $asset)
    {
        $root = $asset->getSourceRoot();
        $path = $asset->getSourcePath();

        $filename = realpath($root . '/' . $path);

        if (file_exists($filename)) {
            touch($filename);
        }

        parent::filterLoad($asset);
    }
}

And you must set "assetic.filter.lessphp.class" in your parameters section (services.yml):

parameters:
    assetic.filter.lessphp.class: Xxx\AssetsBundle\Assetic\Filter\LessphpNonCachedFilter
like image 132
Alex Z. Avatar answered Sep 21 '22 16:09

Alex Z.


AFAIK there's no perfect solution for this yet

I use:

php app/console assetic:dump --watch

which will compile your .less files each time it detects a change in any of the .less files referenced on your templates.

To force a compilation you have to make any change in your "main" file (the file that @imports the others). But, the good news are that is just enough to "touch" the file to do that. So you can just manually touch it every time you need:

touch ~/web/css/main.less;

Or, what i usually do is to set up a script that touches this "main" file each 60 seconds or so:

while true; do
    sleep 60
    touch ~/web/css/main.less
done

This should work on linux and mac.

Hope it helps. At least temporarily :)

like image 34
Miguel Trias Avatar answered Sep 20 '22 16:09

Miguel Trias