1

How can we get Magento 2 base URL in web template (.html file) ?

Note: I am talking about web template (.html file). Not about general templates file (.phtml file).

4
  • 1
    See my note in question. It is not duplicate. Feb 5, 2018 at 5:31
  • @ManashviBirla Please remove duplicate flag. Feb 5, 2018 at 5:37
  • Which HTML file are you making changes to? Feb 5, 2018 at 6:47
  • 1
    Please accept the answer if it's useful to you :) Feb 5, 2018 at 7:08

1 Answer 1

3

=> Set URL in the JS file :

First of all, Add mage/url to the list of dependencies.

Then, Add the following function to the knockout JS file:

getBaseUrl: function() {
    return url.build('<modulename>/<controllername>/<actionname>');
},

=> Use Knockout to set the href :

<a data-bind="attr: { href: getBaseUrl() }">Link Text</a>

Then, clear Varnish and browser cache.

2
  • I have only .html file to access. I don't have JS file access. How can I do ? Feb 5, 2018 at 5:34
  • follow this answer. Add this function in related knockout js file & then add attribute data-bind in your anchor tag as like this format Feb 5, 2018 at 5:43

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