0

I want to be able to access the view url path within javascript so I don't have to worry about linking via pub/var.

How can this be done?

1

Blocks can get the media path using $block->getViewFileUrl('file/path'); and this can then be passed via a template to your javascript through a data-mage-init attribute or a text/x-magento-init script tag.

for example:

<div data-mage-init='
{
   "Namespace_Module/js/script": {
      "mediaUrl": "<?php echo $block->getViewFileUrl() ?>"
   }
}
'></div>
1
  • Yes. That's what I tried. That's a shame, I thought there would be a better way. – LM_Fielding Jul 1 '16 at 9:32

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.