Magento – data-mage-init not working on CMS page

javascriptmagento2

If I add this to a CMS page then it pull in the javascript to the page correctly:

<script type="text/javascript">
    requirejs(['jquery', 'bootstrap'], function (jQuery, jQueryBootstrap) {

    });
</script>

But if I try it with data attribute like so:

<div data-mage-init='{"bootstrap": {}}'>

</div>

Then the javascript is not pulled into the page.

Do data attributes only work in phtml files? Docs seem to say I can use either.

Best Answer

The lack of quotation marks around the configuration part of the init config may be causing a JSON parse error (visible in developer tools javascript console).

<div data-mage-init='{"bootstrap": "{}"}'>
</div>
Related Topic