How to get the URL of an internal static asset located inside of a module's or theme's «web» folder in the context of a block's *.phtml template?

API

Use the \Magento\Framework\View\Element\AbstractBlock::getViewFileUrl() method:

https://github.com/magento/magento2/blob/2ea8cdd7/lib/internal/Magento/Framework/View/Element/AbstractBlock.php#L755-L771

Usage examples

https://github.com/magento/magento2/blob/2ea8cdd7/app/code/Magento/Braintree/view/frontend/templates/form.phtml#L134-L134

https://github.com/magento/magento2/blob/2ea8cdd7/app/code/Magento/Checkout/view/frontend/web/cvv.png

The file will be deployed as a symbolic link into the pub folder:


https://github.com/magento/magento2/blob/2ea8cdd7/app/code/Magento/Catalog/view/frontend/templates/product/view/gallery.phtml#L18-L19

https://github.com/magento/magento2/blob/2ea8cdd7/lib/web/images/loader-1.gif

The file will be deployed as a symbolic link into the pub folder:

A custom file inside a design theme

The file will be deployed as a symbolic link into the pub folder:

So you get it URL wuth the code:

$block->getViewFileUrl('images/test.jpg');