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).

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

=> 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.

  • I have only .html file to access. I don't have JS file access. How can I do ? – Kishan Patadia Feb 5 '18 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 – Rohan Hapani Feb 5 '18 at 5:43

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