Magento 2: How to get a dynamic Base URL Path in XML?

How to get a dynamic Base URL Path in XML?

Hello Magento Devs,

In today’s tutorial, we are going to see how we can get the Base URL path dynamically in any XML File.

In some cases, you are required to get the Base URL and pass it into the XML file.

Instead of passing hardcoded values, we can dynamically get these values and pass them to the XML.

Suppose, You are adding custom breadcrumbs to your account section and you are required to pass the base URL to generate the URL.

You can get a dynamic Base URL using {{baseUrl}} variable in XML.

Let’s consider below example :

/app/code/Jigar/MyAccountBreadCrumbs/view/frontend/layout/customer_account.xml

As shown in the above example, I have prepared the breadcrumbs URL by adding dynamic {{baseUrl}}.

So it will generate the below URL accordingly.

for Home: https://example.com/first_store_view/

for My Account: https://example.com/first_store_view/customer/account

That’s it for today’s tutorial, See you in the next blog.

Thanks, Keep Supporting !!