0

我将 Assetic 与 Twig 一起使用,但不是 symfony2 框架。

这是项目的设置方式

/site
   /template
      /css

/public_html
  /css

原始 css 存储在下面/site/template/css,我希望资产缩小 css 并将其输出到/public_html/css.

这是将资产设置为树枝扩展的方式

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

然后在我的模板中:

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

我可以看到assetic在最终输出中生成了一个唯一的url:

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

但是,如果我查看/public_html/css,文件永远不会生成!

我正在使用带有 apache 的 Windows 7 服务器,PHP 在任何地方写入文件都没有问题。

这可能是什么原因造成的?

4

1 回答 1

3

您在这里所做的是要求资产生成资产的 URL(使用树枝)。

但是资产 url 还没有指向任何文件。您仍然必须使用转储脚本生成它们:

<?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);

此脚本使用树枝模板来定义要转储的文件。当然,您必须定义$templates变量和$twig变量。

于 2012-02-12T13:23:54.527 回答