2012-11-24 31 views
2

讓我開始說我完全不知道應該做什麼,因爲有關Assetic的文檔和可用信息是有限的或面向Symfony的。在Symfony以外使用Assetic的Css重寫器

這是我的文件夾結構。

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

現在,我有以下測試代碼。基本上我克隆了一個乾淨的副本資產和運行composer install。然後我創建一個index.php文件,該文件只需鏈接到我的styles.php文件和HTML標記<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相同。如果我使用Clay先生的CSS URI重寫器,我會得到預期的輸出結果。

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

那麼,我在做什麼錯誤的資產?我不知道我應該傳遞什麼路徑到哪裏。

謝謝。

回答

2

這只是一個相當艱難的時期,但最後(在閱讀webassets的文檔後,基於資產的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(); 

我不知道是很清楚,所以問,如果你不明白的東西。

+0

我仍然無法使它工作。目標路徑,您的意思是絕對路徑或URL。從查看Assetic的來源,它似乎要求提供資產目標的URL。 –

+0

我把dirname($ assetPath)放在哪裏,它應該是包含css文件的目錄的絕對路徑。 '/ assets/css'是資產目錄的URL。 $ asset-> setTargetPath('/ path/to/dumped/asset');這裏是包含轉儲資產的目錄的絕對路徑。 – chadrien

相關問題