Question

I am using Assetic with Twig but not the symfony2 framework.

Here's how the project is set up

/site
   /template
      /css

/public_html
  /css

The raw css is stored under /site/template/css and I want assetic to minify the css and output it to /public_html/css.

Here's how assetic is set up as a twig extension

$factory = new AssetFactory(//absolute path to `/site/template/`);
$factory->setDefaultOutput(//absolute path to `/public_html/`);
$factory->setDebug(false);
$twig->addExtension(new AsseticExtension($factory));

Then in my template:

{% stylesheets 'css/screen.css'  output='css/*' %}
  <link href="{{ asset_url }}" type="text/css" rel="stylesheet" />
{% endstylesheets %}

I can see that assetic has generated a unique url in the final output:

<link href="css/00da241.css" type="text/css" rel="stylesheet" />

However, if I look in /public_html/css, the files are never generated!

I am using a Windows 7 server with apache and PHP has no issues writing files anywhere.

What could be causing this?

Was it helpful?

Solution

What you've done here is asking assetic to generate URL's to assets (using twig).

But assets urls don't point to any file yet. You still have to generate them, using a dump script:

<?php

use Assetic\AssetWriter;
use Assetic\Extension\Twig\TwigFormulaLoader;
use Assetic\Extension\Twig\TwigResource;
use Assetic\Factory\LazyAssetManager;

$am = new LazyAssetManager($factory);

// enable loading assets from twig templates
$am->setLoader('twig', new TwigFormulaLoader($twig));

// loop through all your templates
foreach ($templates as $template) {
    $resource = new TwigResource($twigLoader, $template);
    $am->addResource($resource, 'twig');
}

$writer = new AssetWriter('/path/to/web');
$writer->writeManagerAssets($am);

This script uses twig templates to define which files to dump. You will have to define the $templates variable and the $twig variables of course.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top