0

From what I've read there are 2 ways to include your javascript files:

1) In catalog_product_view.xml you insert <link src="js/product.js"/> and in web/js/product.js you add your js code specific to the product page (old magento1 way).

2) The second way (the way magento2 does it) items.phtml (or whatever phtml file) you add

<script>
    require([
        'jquery'
    ], function ($) {
        //do something specific here 
        });
</script>

Why do they through php, html and javascript in a single phtml file? I've never seen that before in another platform. Especially if your javascript code is big you don't want to have it there.

And for example if you put it in another separate file, like product.js, isn't it better to include it via xml then to do this in phtml:

<script>
    require([
        'product'
    ], function ($) {
        //do nothing here because it is done in product.js
        });
</script>
-1

The require call doesn't have to be inline (phtml), the require could be in a separate js file included in the normal fashion.

1
  • normal fashion means xml? I already said you can do that – Claudiu Creanga Jun 16 '16 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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