How to passing Dynamic arguments from template to RequireJs in magento 2?

Magento template support script tag with X-MAGENTO-INIT declaration. You can pass dynamic PHP values to Javascript from a template file.

create demo.phtml file in your module to pass dynamic PHP value to Javascript,

Now We can fetch Base Url and Store Id in javascript file using require js. We have to create requirejs-example.js file to get both of the dynamic values.


We have passed the argument as config for the function.

Always return function takes two optional parameters.

The first parameter contains the configuration value which we have passed from the phtml.
The second parameter contains the DOM node element.

You can get store id and baseURL in javascript using the above way.