Adding JavaScript or CSS Files to Custom Two Column Left Pages in Magento
Magento provides a robust layout system for managing the appearance of your storefront. In this post, we’ll explore how to add custom JavaScript (JS) and Cascading Style Sheets (CSS) files to a two column left page using the page.xml file.
Understanding the Problem
You’re currently facing an issue where you want to include a JS file named sidebar_menu.js in your two column left layout. You’ve tried adding the reference within a <reference name="head"> block, but it doesn’t seem to be working as expected. Let’s troubleshoot this.
The Current Approach: Adding References via XML
In Magento 2, the layout system uses XML files for defining page layouts and adding references to blocks, CSS, and JS files. You’re on the right track by trying to add a reference in your page.xml file.
The relevant part of your code is:
<reference name="head">
<action method="addJs"><script>sidebar_menu.js</script></action>
</reference>
This code attempts to add the sidebar_menu.js script within the <head> block. The expectation is that this JS file will be loaded on every page using this layout.
Why Your Current Approach Isn’t Working
The issue lies in the fact that Magento 2 uses a specific naming convention for its built-in layout handles and blocks. The <reference name="head"> block you’re targeting is used by default layouts, but it may not always be available or active depending on your custom theme’s configuration.
A Better Approach: Using the default_head_blocks Handle
Instead of targeting the general <head> block directly, we should use one of Magento 2âs built-in handles. The default_head_blocks handle is a parent handle for all head blocks, ensuring that our JS file will be loaded consistently across layouts.
Let’s modify your XML:
<page_two_columns_left translate="label">
<label>All Two-Column Layout Pages (Left Column)</label>
<reference name="default_head_blocks">
<action method="addJs"><script>sidebar_menu.js</script></action>
</reference>
<reference name="root">
<action method="setTemplate"><template>page/2columns-left.phtml</template></action>
<action method="setIsHandle"><applied>1</applied></action>
</reference>
</page_two_columns_left>
In this updated code, we’re wrapping the <addJs> action within a <reference name="default_head_blocks"> block. This ensures that our sidebar_menu.js file is loaded in all layouts that use the two column left layout.
Incorporating CSS Files
The process for adding CSS files is similar but uses the addCss method instead:
<reference name="default_head_blocks">
<action method="addCss"><stylesheet>path/to/your.css</stylesheet></action>
</reference>
Replace path/to/your.css with the actual path to your CSS file.
Conclusion
In Magento 2, adding custom JS and CSS files requires a nuanced understanding of layout handles. By targeting specific built-in handles like default_head_blocks, we can ensure that our resources are loaded consistently across different layouts.
Always remember to make changes in your theme’s local XML files (like page.xml) to avoid losing customizations during updates. You can find more detailed guides on Magento 2 layout management in the official documentation and via Laravel Company’s Magento development resources.
Don’t hesitate to experiment with different layouts and handles to find the perfect fit for your store’s needs. Happy coding!