0

I want to create an extension which contains an AssetBundle, which provides a simple static CSS file. In that static CSS file, I want to use an image file, which is also part of the extension.

How can I know the image file's URL so I can use it inside the CSS file?

If I register the image file as an asset, it will get a random URL that we cannot predict! So it would be impossible to do something like:

.some-selector { background: url('/assets/?????/image.jpg'); }

So, how can this be done?


For further clarification, here is an example folder structure of such extension:

  • extension/Widget.php - some widget that registers our AssetBundle
  • extension/AssetBundle.php - asset bundle that registers the css
  • extension/assets/css/style.css - the css
  • extension/assets/images/image.jpg - the image we want to use inside style.css
8
  • What was your expected result? Commented Mar 28, 2017 at 6:01
  • @LIJINSAMUEL Having a purely static CSS file which uses an image provided by the extension. Commented Mar 28, 2017 at 6:03
  • Images in the assets folder? Commented Mar 28, 2017 at 6:04
  • .some-selector { background: url('assets/image.jpg'); } Commented Mar 28, 2017 at 6:05
  • @LIJINSAMUEL No, the assets are in the extension's vendor folder, so they are not going to be in the application's assets folder. Commented Mar 28, 2017 at 6:06

1 Answer 1

1

In this case I would recommend to include also a new css files into the asset definition. In this case the image and css file will be in the assets folder and you can specify relative path, not absolute. e.g. /assets/a4dc56/image.jpg

/assets/a4dc56/style.css with the following content:

.some-selector {
    background: url('image.jpg');
}
Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant. I knew there had to be an obvious solution. I ended up including the image through a relative path as such: background: url('../images/image.jpg');
How about using AssetManager just like: $bundle = AppAsset::register($this); then: <img src="<?= $bundle->baseUrl.'/logo.png' ?>" />?

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.