I want to add external JavaScript library in my module. It looks something like below script code

src="<?php echo $block->escapeUrl($block->getJsLibUrl());?>" 
data-min="<?php echo (int)$block->getMinLimit(); ?>"    
data-max="<?php echo (int)$block->getMaxLimit(); ?>" async>

Here, the script URL is dynamic and data min/max values too.

I am not sure how I can add this external js library with data attributes in Magento 2 standard way. Is it fine if I will add this script in .phtml file? or is there any alternate way to add using requireJS?

Thanks for your attention and help in advance. Feel free to ask if more details are needed.

1 Answer 1


yes, you can do this way. Magento has done the similar things in number of places for example look at vendor/magento/module-catalog/view/adminhtml/templates/product/edit/tabs.phtml in your magento2 core folder. This is just one example, there are several others too. So, I think this isn't a bad practice if Magento is following it too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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