1

I've added some images to my module eg.

view/base/web/images/some-image.png

Having run bin/magento setup:static-content:deploy I now see these have been published to the pub directory

pub/static/frontend/Magento/luma/en_US/Moxune_Moxune/images/some-image.png

I can access these from the web

https://mage2.local/static/frontend/Magento/luma/en_US/Moxune_Moxune/images/some-image.png

My question

What is the proper way to refer to these files in a .phtml file in my module?

It seems like putting the path from /static forward would be pretty hacky. My guess is there's some way to refer to the module and start off with /images or similar and provide a relative path to the files, but I'm not sure where to look for the answer.

1

Wow, a few minutes later and I've found the answer. I'll leave this up as a Q/A style post as I think it could be helpful for other folks.

$block->getViewFileUrl('Moxune_Moxune/images/some-image.png')

FWIW I started off reading this article from Alan Storm; it just didn't have the bit about how to refer to static assets like image files from templates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.