kriswallsmith / assetic

Asset Management for PHP
MIT License
3.75k stars 555 forks source link

Documentation for Twig dump script unclear #456

Open jmcclell opened 11 years ago

jmcclell commented 11 years ago

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.

stof commented 11 years ago

$twigLoader should be your Twig_Loader object

broncha commented 10 years ago

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 commented 10 years ago

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

agiannis commented 8 years ago

+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');