assetic icon indicating copy to clipboard operation
assetic copied to clipboard

Documentation for Twig dump script unclear

Open jmcclell opened this issue 11 years ago • 4 comments

The current documentation for the Twig based dump script is hard (if not impossible) to follow. See PHP comments below:

<?php

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

// Why LazyAssetManager? Unclear on its purpose.
$am = new LazyAssetManager($factory);

// What is $twig? Twig Environment?
$am->setLoader('twig', new TwigFormulaLoader($twig));

// Where does $templates come from? Are these just paths?
foreach ($templates as $template) {
    // What is $twigLoader? Is it the loader I set above? Doing this gives me exceptions.
   /*
    *PHP Fatal error:  Call to undefined method Assetic\Extension\Twig\TwigFormulaLoader::getSource() in /var/.../Assetic/src/Assetic/Extension/Twig/TwigResource.php on line 35
    */
    $resource = new TwigResource($twigLoader, $template);
    $am->addResource($resource, 'twig');
}

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

Could you please clarify the documentation in regards to Twig? It would be extremely helpful.

jmcclell avatar Jun 14 '13 02:06 jmcclell

$twigLoader should be your Twig_Loader object

stof avatar Jun 15 '13 10:06 stof

Hi, I have this setup. The asset_url is being generated correctly, but the files are not being dumped. What am I missing!


$loader = new \Twig_Loader_Filesystem($paths);
$twig = new \Twig_Environment($loader, array(
    'cache' =>  $app.'/themes/cache'
));

//asset factory
$assetFactory = new AssetFactory($app."/themes/".$theme );
$assetFactory->setDebug($this->container->getParameter('kernel.debug'));
$assetFactory->setAssetManager(new AssetManager());
$assetFactory->setFilterManager(new FilterManager());

$am = new LazyAssetManager($assetFactory);
$am->setLoader('twig', new TwigFormulaLoader($twig));

$finder = new Finder();
$finder
    ->files()
    ->in($app."/themes/".$theme)
    ->exclude('css')
    ->exclude('js')
    ->exclude('images')
    ->name("*twig")
;

foreach($finder as $template){
    $resource = new TwigResource($loader, $template);
    $am->addResource($resource, 'twig');
}

$writer = new AssetWriter($app.'../web');
$writer->writeManagerAssets($am);

$twig->addExtension(new AsseticExtension($assetFactory));

broncha avatar Jun 21 '14 12:06 broncha

Okay, I got it working. It was the $template, which was not string. I got it working by changing $resource = new TwigResource($loader, $template); to $resource = new TwigResource($loader, $template->getFileName());

But debug mode is not being considered here. I have 2 stylesheets in the stylesheets tag, the links are being generated correctly, but only 1 file is being generated!!

{% stylesheets
        'css/bootstrap.css'
        'css/shop-homepage.css'
%}

<link href="themes/{{ asset_url }}" rel="stylesheet">
{% endstylesheets %}
<link href="themes/css/706c876_bootstrap_1.css" rel="stylesheet">
<link href="themes/css/706c876_shop-homepage_2.css" rel="stylesheet">

But I have only 1 file 706c876.css generated

broncha avatar Jun 21 '14 15:06 broncha

+1 for adding more details in the Twig Part of the documentation.

If anyone want to set Assetic and Silex . This is how you could do it

$resource = new TwigResource($app[twig.loader], 'relative/path/to/template/file');

agiannis avatar Mar 10 '16 14:03 agiannis