Yii 2: Using a module image declared as an asset
I have an image in a module in the following structure: vendor / myvendorname / mymodulename / assets / img / delete-icon.png
I need to add <img>
to a page using JavaScript and it can have an attribute src
indicating this delete-icon.png
.
$("#delete").attr("src", "?");
How can I link to an image if it will be placed in the Yii generated resource directory? What is the way to get this path?
+3
source to share
1 answer
Once you register AssetBundle
, you can get it baseUrl
. For the rest of the view, you can use this to get images:
$assets = MyAssetBundle::register($this);
$imagePath = $assets->baseUrl . '/img/delete-icon.png';
$this->registerJS(<<<JS
$("#delete").attr("src", "$imagePath");
JS
);
+4
source to share