6

I am using the following code to publish my css file to assets.

Yii::app()->clientScript->registerCssFile( Yii::app()->assetManager->publish( Yii::getPathOfAlias('application').'/../css/combo-autocomplete.css' ));

It registers the css successfully and styles are working fine, but the following css class using a image.

.ui-icon {
   background-image: url(../images/down-arrow.png); 
}

is not loading, it is referred to path inside assets folder. It is looking for the image inside

root/assets/images/

Is there any way to redirect the image url in the css to the original path, because i am not registering my images as assets as of now.

root/images

3
  • 1
    It doesn't make that much sense to publish your CSS as an asset but leave your images bare. Either you are designing a self-contained component (in which case publish everything) or you don't (in which case publish nothing). Commented Oct 18, 2012 at 12:13
  • @Jon, if i publish images to assets, it will not keep the same folder name (images) inside my assets folder. it will be like /root/assets/4s431e/icon.png ? Commented Oct 18, 2012 at 12:19
  • I expanded the comment into an answer. Commented Oct 18, 2012 at 12:33

1 Answer 1

11

Yii's asset manager is designed to let you package components such as UI widgets in a self-contained manner (the alternative being to manually distribute various files in different places inside your application directory structure). So if it makes sense to publish anything as an asset from your component, it makes sense to publish everything as an asset.

In that case, you should structure your component e.g. as

component/
    assets/
        css/
        images/
        js/

Then, publish the whole assets/ directory using Yii's asset manager instead of publishing files one by one. If you do this, they will end up being published in a manner like

assets/
    random_hash/
        css/
        images/

Your CSS can then simply refer to images with url(../images/image.jpg) without needing to know the value of random_hash.

Sign up to request clarification or add additional context in comments.

1 Comment

My css not not sees images (( In random_hash/ is only css file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.