2

首先让我说我完全不知道我该做什么,因为关于 Assetic 的文档和可用信息要么是有限的,要么是面向 Symfony 的。

这是我的文件夹结构。

Assetic
   + assets
      + css
         + example.css
   + docs
   + src
   + tests
   + vendor
   + index.php
   + styles.php

现在,我有以下测试代码。基本上我克隆了一份 Assetic 的干净副本并运行了composer install. 然后我创建了一个文件,它只是用 HTMLs标签index.php链接到我的文件。styles.php<link>

这是我的styles.php

<?php

require 'vendor/autoload.php';

$assetPath = __DIR__.'/assets/css/example.css';
$assetBasePath = __DIR__.'/assets/css';

$asset = new Assetic\Asset\FileAsset($assetPath, array(), $assetBasePath, 'example.css');

header('Content-Type: text/css');

$asset->setTargetPath(__DIR__);

echo $asset->dump(new Assetic\Filter\CssRewriteFilter);

这是我的example.css样式表。

body {
    background-image: url('../img/background.png');
}

当我styles.php在浏览器中加载时,我得到以下输出。

url('../img/background.png');

这与实际的 CSS 相同。如果我使用来自 Mr. Clay 的 CSS URI Rewriter,我会得到预期的输出。

url('/Assetic/assets/img/background.png');

那么我对 Assetic 做错了什么?我不知道我应该通过什么路径以及去哪里。

谢谢。

4

1 回答 1

2

只是很难用它,但最后(在阅读了 webassets 的文档后,assetic 所基于的 python 库)我克服了缺乏文档的问题。

干得好

<?php

require 'vendor/autoload.php';

$assetPath = __DIR__.'/assets/css/example.css';

$asset = new Assetic\Asset\FileAsset($assetPath, array(new Assetic\Filter\CssRewriteFilter), dirname($assetPath), '/assets/css');
// I assume the 'assets' directory is at the root of your website

header('Content-Type: text/css');

$asset->setTargetPath('/path/to/dumped/asset');
// As above, it's the path to the generated asset from your http root

echo $asset->dump();

我不确定是否很清楚,所以请问您是否有不明白的地方。

于 2012-11-28T16:45:30.250 回答