In this article, we will be covering the basics of Magento layout XML.
We will go over local.xml
and do some basic modifications. This will include adding and removing scripts, removing blocks and adding layout changes.
Now that we have a basic understanding of the theme hierarchy from the first article in this series, we will dive a little deeper and explain the templating files.
The templating files consist of two sub-directories of which are:
app/design/frontend/<package_name>/<theme_name>/layout/
app/design/frontend/<package_name>/<theme_name>/template/
There is a lot to cover, for this reason I will be splitting them out into their own articles. Today we will just be covering the layout aspect. The template aspect will come into play in the next article.
Wait, before we even get started we need to do one vital thing which is to disable Magento cache, if you haven't already done so, that is! In doing so, it will allow us to view our modifications instantly rather than having to refresh the cache each time we make a change. Ideally it should be turned off during development of a site. To do this log into the admin area and head over to system > cache management and disable all.
Now that's sorted let's begin.
The layout folder contains the XML files that largely dictates what is displayed on the front end of the store. The layout structure is quite complex in Magento, yet this is one of the reasons that makes it so powerful and flexible.
You will find hundreds of XML files each doing their own thing, each view or module in app/code/
gets it's layout specified by its own XML file. If you ever install a third party module onto your store and it affects the front end it will no doubt have its own XML file.
So, how do I know which file to edit if I need to?
Well, the naming convention of the files makes it easier to track down when you need to make modifications. For example, Magento app/code/core/Mage/Page/
module has its own XML file named app/design/frontend/base/default/layout/page.xml
as you can see there's a pattern starting to emerge here! Once you have familiarized yourself and done a few stores you will soon notice some repetitiveness and recall which files you need to edit.
Note: Please be aware of third party modules, as technically the developer can name their XML file anything they desire. In this scenario, unless it's in their documentation, you will have to hunt down the name of the file within the module itself usually found in the config.xml
file. Also note not all modules have an XML file, usually the XML file will only be present if it affects the front end of the store so don't expect one all the time!
Path to config: app/code/local/<name_space>/<module>/etc/config.xml
Notice I have referenced base/default
above, remember these are where the core files reside, if you need to make modifications always copy it over to your own package/theme
never edit base/default
files.
Like so:
app/design/frontend/base/default/layout/page.xml
copy to:
app/design/frontend/<package_name>/<theme_name>/layout/page.xml
Heavily modifying these files does require experience and with this being a
beginners tutorial, it's beyond the scope of this series; however, I will
run through local.xml
and how this ties in with theme development as well as covering a handful of basic modifications which I think will be useful.
Put simply, it's a file that's placed within our theme layout folder that will house a large portion of our overrides or updates to XML references for that theme. It's considered good practice and Magento recommends it. We could look at it as Magento version of WordPress functions.php
file.
Wait, a "large portion" why not "all" of our overrides or updates?
There is a debate on this topic and if we did a bit of research we will find everyone has their own opinion. Some will say only use local.xml
and don't make edits anywhere else while others will disagree, so don't take the following set in stone.
Personally I think it's a great place for small modifications, such as adding blocks, removing blocks or changing templates. It isn't a place to completely layout your product page or the like, if you want to do that, do it in the relevant file e.g catalog.xml
Yeah, it might save us a bit of time when we upgrade Magento as all our edits are in one single file but getting all our edits in one single file in the first place, with all the headaches of trying to override other XML files, it simply gets outweighed.
Furthermore when were doing a large amount of modifications to a page we ideally want to reference the other XML that's part of that page, we would have to constantly switch between the two files, and in the end were splitting up functionality between two separate files - not what we really want!
So, how to set it up...
Create the local.xml
file inside our theme layout folder app/design/frontend/<package_name>/default/layout/local.xml
and add some basic XML markup structure:
<?xml version="1.0"?> <!-- /* * Store Name * Store URL * * @description Layout modifications * @author Author Name * @package packagename_default * */ --> <layout version="0.1.0"> <!-- our modifications will go here --> </layout>
Now that we have our file ready I will show you a handful of common techniques.
A very common modification is to add or remove JavaScript and CSS. If your working with jQuery you will have to add this in as Magento does not include it by default and any custom JavaScript you require will also need adding in.
If we view source on a Magento installation we will see a whole bunch of JavaScript being pulled in, some of which we won't use, in which case it needs removing as its an unnecessary http
request - Magento isn't quick so let's do the basics right!
To include a file, we need to decide if it's going to be global e.g on every page of our store or just certain areas. With this we can select the correct layout handle to use.
I will introduce two layout handles, <default>
and <cms_index_index>
. Of course, there are many more available to us, but for now lets focus on just these.
The <default>
handle is global and will affect all pages while the <cms_index_index>
will only affect the home page.
Now, on to the code.
<?xml version="1.0"?> <layout version="0.1.0"> <default> <reference name="head"> <!-- jQuery locally --> <action method="addItem"><type>skin_js</type><name>js/libs/jquery.min.js</name></action> <!-- jQuery CDN --> <block type="core/text" name="cdn.jquery"> <action method="setText"> <text> <![CDATA[ <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript">jQuery.noConflict();</script> ]]> </text> </action> </block> <!-- add some items (globally) --> <action method="addItem"><type>skin_js</type><name>js/libs/modernizr.min.js</name></action> <action method="addItem"><type>skin_js</type><name>js/libs/html5shiv.min.js</name><params/><if>lt IE 9</if></action> <action method="addItem"><type>skin_js</type><name>js/libs/respond.min.js</name><params/><if>lt IE 9</if></action> <action method="addItem"><type>skin_js</type><name>js/libs/selectivizr.min.js</name><params/><if>lt IE 9</if></action> <!-- remove some items (globally) --> <action method="removeItem"><type>skin_css</type><name>css/widgets.css</name></action> <action method="removeItem"><type>skin_css</type><name>css/print.css</name></action> <action method="removeItem"><type>skin_css</type><name>css/styles-ie.css</name><params/><if>lt IE 8</if></action> <action method="removeItem"><type>skin_js</type><name>js/ie6.js</name><if>lt IE 7</if></action> <action method="removeItem"><type>js</type><name>lib/ds-sleight.js</name><params/><if>lt IE 7</if></action> </reference> </default> <cms_index_index> <reference name="head"> <!-- add items just on the homepage --> <action method="addItem"><type>skin_js</type><name>js/libs/home.min.js</name></action> <action method="addItem"><type>skin_css</type><name>css/home.css</name></action> </reference> </cms_index_index> </layout>
There's quite a lot going on here but when broken down it's relatively straight forward.
<action method="{addItem or removeItem}"> <type>{type of file / location}</type> <name>{path to the file}</name> </action>
Following up on point two: It also dictates where the file is in the hierarchy, each type references a different position in the hierarchy which gets prepended to what you enter in the <name>
field. I've listed them below for reference:
skin/frontend/<package_name>/default/{name}
skin/frontend/<package_name>/default/{name}
js/{name}
Notice that loading a file from an external source such as a CDN has a slightly different syntax. Also it's important to include the jQuery.noConflict();
at the end to avoid jQuery conflicting with Magento built in Prototype library.
Magento comes bundled with multiple sidebar blocks that let's face it we are never going use in a real life situation, such as the "Back to School" advert. Below are two methods we can use:
<?xml version="1.0"?> <layout version="0.1.0"> <default> <!-- remove a block --> <remove name="right.permanent.callout" /> <!-- unset a block --> <reference name="right"> <action method="unsetChild"><name>right.poll</name></action> </reference> </default> </layout>
The remove method is a good way to remove a block regardless of which layout handle loaded the block, sometimes we just want it gone globally no matter where it is and to never return!
On the other hand unsetChild will only remove a block from within a specific layout handle. You can see I'm calling it within the right layout handle so it will only be removed from there. If the right.poll block is called from anywhere else in a different layout handle it will get displayed.
Here we have an example of adding in an additional structural block to the homepage. We are referencing the content block and using the after
tag to specify the block to display at the end of the content block.
<?xml version="1.0"?> <layout version="0.1.0"> <cms_index_index> <reference name="content"> <block type="core/template" name="home.additional" after="-" template="/home/additional.phtml" /> </reference> </cms_index_index> </layout>
Lastly we have an example of adding in a static CMS block, but first you'll need to create one for this to work.
Once logged into the admin area head over to CMS > Static Blocks and add a new block. Take note of the "Identifier" as we need to reference this in the XML code.
<?xml version="1.0"?> <layout version="0.1.0"> <cms_index_index> <reference name="content"> <block type="cms/block" name="home.static.block" after="-"> <action method="setBlockId"><block_id>home_static_block</block_id></action> </block> </reference> </cms_index_index> </layout>
Within the block_id
is where we enter our identifier.
We have barely scratched the surface and there are many other uses for XML not to mention more layout handles and XML tags available to us. Magento layout itself warrants to be a series as there is a great deal to cover, for now I am keeping it to just the basics.
If you want to do some more reading up on the XML I would recommend reading this article and also download a copy of Magento Official Design Guide which goes into more depth and has a good explanation of the other XML tags we can use.
In the next article, we're going to push forward and look into the template files.
The Best Small Business Web Designs by DesignRush
/Create Modern Vue Apps Using Create-Vue and Vite
/Pros and Cons of Using WordPress
/How to Fix the “There Has Been a Critical Error in Your Website” Error in WordPress
/How To Fix The “There Has Been A Critical Error in Your Website” Error in WordPress
/How to Create a Privacy Policy Page in WordPress
/How Long Does It Take to Learn JavaScript?
/The Best Way to Deep Copy an Object in JavaScript
/Adding and Removing Elements From Arrays in JavaScript
/Create a JavaScript AJAX Post Request: With and Without jQuery
/5 Real-Life Uses for the JavaScript reduce() Method
/How to Enable or Disable a Button With JavaScript: jQuery vs. Vanilla
/How to Enable or Disable a Button With JavaScript: jQuery vs Vanilla
/Confirm Yes or No With JavaScript
/How to Change the URL in JavaScript: Redirecting
/15+ Best WordPress Twitter Widgets
/27 Best Tab and Accordion Widget Plugins for WordPress (Free & Premium)
/21 Best Tab and Accordion Widget Plugins for WordPress (Free & Premium)
/30 HTML Best Practices for Beginners
/31 Best WordPress Calendar Plugins and Widgets (With 5 Free Plugins)
/25 Ridiculously Impressive HTML5 Canvas Experiments
/How to Implement Email Verification for New Members
/How to Create a Simple Web-Based Chat Application
/30 Popular WordPress User Interface Elements
/Top 18 Best Practices for Writing Super Readable Code
/Best Affiliate WooCommerce Plugins Compared
/18 Best WordPress Star Rating Plugins
/10+ Best WordPress Twitter Widgets
/20+ Best WordPress Booking and Reservation Plugins
/Working With Tables in React: Part Two
/Best CSS Animations and Effects on CodeCanyon
/30 CSS Best Practices for Beginners
/How to Create a Custom WordPress Plugin From Scratch
/10 Best Responsive HTML5 Sliders for Images and Text… and 3 Free Options
/16 Best Tab and Accordion Widget Plugins for WordPress
/18 Best WordPress Membership Plugins and 5 Free Plugins
/25 Best WooCommerce Plugins for Products, Pricing, Payments and More
/10 Best WordPress Twitter Widgets
1 /12 Best Contact Form PHP Scripts for 2020
/20 Popular WordPress User Interface Elements
/10 Best WordPress Star Rating Plugins
/12 Best CSS Animations on CodeCanyon
/12 Best WordPress Booking and Reservation Plugins
/12 Elegant CSS Pricing Tables for Your Latest Web Project
/24 Best WordPress Form Plugins for 2020
/14 Best PHP Event Calendar and Booking Scripts
/Create a Blog for Each Category or Department in Your WooCommerce Store
/8 Best WordPress Booking and Reservation Plugins
/Best Exit Popups for WordPress Compared
/Best Exit Popups for WordPress Compared
/11 Best Tab & Accordion WordPress Widgets & Plugins
/12 Best Tab & Accordion WordPress Widgets & Plugins
1 /New Course: Practical React Fundamentals
/Preview Our New Course on Angular Material
/Build Your Own CAPTCHA and Contact Form in PHP
/Object-Oriented PHP With Classes and Objects
/Best Practices for ARIA Implementation
/Accessible Apps: Barriers to Access and Getting Started With Accessibility
/Dramatically Speed Up Your React Front-End App Using Lazy Loading
/15 Best Modern JavaScript Admin Templates for React, Angular, and Vue.js
/15 Best Modern JavaScript Admin Templates for React, Angular and Vue.js
/19 Best JavaScript Admin Templates for React, Angular, and Vue.js
/New Course: Build an App With JavaScript and the MEAN Stack
/Hands-on With ARIA: Accessibility Recipes for Web Apps
/10 Best WordPress Facebook Widgets
13 /Hands-on With ARIA: Accessibility for eCommerce
/New eBooks Available for Subscribers
/Hands-on With ARIA: Homepage Elements and Standard Navigation
/Site Accessibility: Getting Started With ARIA
/How Secure Are Your JavaScript Open-Source Dependencies?
/New Course: Secure Your WordPress Site With SSL
/Testing Components in React Using Jest and Enzyme
/Testing Components in React Using Jest: The Basics
/15 Best PHP Event Calendar and Booking Scripts
/Create Interactive Gradient Animations Using Granim.js
/How to Build Complex, Large-Scale Vue.js Apps With Vuex
1 /Examples of Dependency Injection in PHP With Symfony Components
/Set Up Routing in PHP Applications Using the Symfony Routing Component
1 /A Beginner’s Guide to Regular Expressions in JavaScript
/Introduction to Popmotion: Custom Animation Scrubber
/Introduction to Popmotion: Pointers and Physics
/New Course: Connect to a Database With Laravel’s Eloquent ORM
/How to Create a Custom Settings Panel in WooCommerce
/Building the DOM faster: speculative parsing, async, defer and preload
1 /20 Useful PHP Scripts Available on CodeCanyon
3 /How to Find and Fix Poor Page Load Times With Raygun
/Introduction to the Stimulus Framework
/Single-Page React Applications With the React-Router and React-Transition-Group Modules
12 Best Contact Form PHP Scripts
1 /Getting Started With the Mojs Animation Library: The ShapeSwirl and Stagger Modules
/Getting Started With the Mojs Animation Library: The Shape Module
/Getting Started With the Mojs Animation Library: The HTML Module
/Project Management Considerations for Your WordPress Project
/8 Things That Make Jest the Best React Testing Framework
/Creating an Image Editor Using CamanJS: Layers, Blend Modes, and Events
/New Short Course: Code a Front-End App With GraphQL and React
/Creating an Image Editor Using CamanJS: Applying Basic Filters
/Creating an Image Editor Using CamanJS: Creating Custom Filters and Blend Modes
/Modern Web Scraping With BeautifulSoup and Selenium
/Challenge: Create a To-Do List in React
1 /Deploy PHP Web Applications Using Laravel Forge
/Getting Started With the Mojs Animation Library: The Burst Module
/10 Things Men Can Do to Support Women in Tech
/A Gentle Introduction to Higher-Order Components in React: Best Practices
/Challenge: Build a React Component
/A Gentle Introduction to HOC in React: Learn by Example
/A Gentle Introduction to Higher-Order Components in React
/Creating Pretty Popup Messages Using SweetAlert2
/Creating Stylish and Responsive Progress Bars Using ProgressBar.js
/18 Best Contact Form PHP Scripts for 2022
/How to Make a Real-Time Sports Application Using Node.js
/Creating a Blogging App Using Angular & MongoDB: Delete Post
/Set Up an OAuth2 Server Using Passport in Laravel
/Creating a Blogging App Using Angular & MongoDB: Edit Post
/Creating a Blogging App Using Angular & MongoDB: Add Post
/Introduction to Mocking in Python
/Creating a Blogging App Using Angular & MongoDB: Show Post
/Creating a Blogging App Using Angular & MongoDB: Home
/Creating a Blogging App Using Angular & MongoDB: Login
/Creating Your First Angular App: Implement Routing
/Persisted WordPress Admin Notices: Part 4
/Creating Your First Angular App: Components, Part 2
/Persisted WordPress Admin Notices: Part 3
/Creating Your First Angular App: Components, Part 1
/How Laravel Broadcasting Works
/Persisted WordPress Admin Notices: Part 2
/Create Your First Angular App: Storing and Accessing Data
/Persisted WordPress Admin Notices: Part 1
/Error and Performance Monitoring for Web & Mobile Apps Using Raygun
/Using Luxon for Date and Time in JavaScript
7 /How to Create an Audio Oscillator With the Web Audio API
/How to Cache Using Redis in Django Applications
/20 Essential WordPress Utilities to Manage Your Site
/Introduction to API Calls With React and Axios
/Beginner’s Guide to Angular 4: HTTP
/Rapid Web Deployment for Laravel With GitHub, Linode, and RunCloud.io
/Beginners Guide to Angular 4: Routing
/Beginner’s Guide to Angular 4: Services
/Beginner’s Guide to Angular 4: Components
/Creating a Drop-Down Menu for Mobile Pages
/Introduction to Forms in Angular 4: Writing Custom Form Validators
/10 Best WordPress Booking & Reservation Plugins
/Getting Started With Redux: Connecting Redux With React
/Getting Started With Redux: Learn by Example
/Getting Started With Redux: Why Redux?
/Understanding Recursion With JavaScript
/How to Auto Update WordPress Salts
/How to Download Files in Python
/Eloquent Mutators and Accessors in Laravel
1 /10 Best HTML5 Sliders for Images and Text
/Site Authentication in Node.js: User Signup
/Creating a Task Manager App Using Ionic: Part 2
/Creating a Task Manager App Using Ionic: Part 1
/Introduction to Forms in Angular 4: Reactive Forms
/Introduction to Forms in Angular 4: Template-Driven Forms
/24 Essential WordPress Utilities to Manage Your Site
/25 Essential WordPress Utilities to Manage Your Site
/Get Rid of Bugs Quickly Using BugReplay
1 /Manipulating HTML5 Canvas Using Konva: Part 1, Getting Started
/10 Must-See Easy Digital Downloads Extensions for Your WordPress Site
/22 Best WordPress Booking and Reservation Plugins
/Understanding ExpressJS Routing
/15 Best WordPress Star Rating Plugins
/Creating Your First Angular App: Basics
/Inheritance and Extending Objects With JavaScript
/Introduction to the CSS Grid Layout With Examples
1Performant Animations Using KUTE.js: Part 5, Easing Functions and Attributes
Performant Animations Using KUTE.js: Part 4, Animating Text
/Performant Animations Using KUTE.js: Part 3, Animating SVG
/New Course: Code a Quiz App With Vue.js
/Performant Animations Using KUTE.js: Part 2, Animating CSS Properties
Performant Animations Using KUTE.js: Part 1, Getting Started
/10 Best Responsive HTML5 Sliders for Images and Text (Plus 3 Free Options)
/Single-Page Applications With ngRoute and ngAnimate in AngularJS
/Deferring Tasks in Laravel Using Queues
/Site Authentication in Node.js: User Signup and Login
/Working With Tables in React, Part Two
/Working With Tables in React, Part One
/How to Set Up a Scalable, E-Commerce-Ready WordPress Site Using ClusterCS
/New Course on WordPress Conditional Tags
/TypeScript for Beginners, Part 5: Generics
/Building With Vue.js 2 and Firebase
6 /Best Unique Bootstrap JavaScript Plugins
/Essential JavaScript Libraries and Frameworks You Should Know About
/Vue.js Crash Course: Create a Simple Blog Using Vue.js
/Build a React App With a Laravel RESTful Back End: Part 1, Laravel 5.5 API
/API Authentication With Node.js
/Beginner’s Guide to Angular: Routing
/Beginners Guide to Angular: Routing
/Beginner’s Guide to Angular: Services
/Beginner’s Guide to Angular: Components
/How to Create a Custom Authentication Guard in Laravel
/Learn Computer Science With JavaScript: Part 3, Loops
/Build Web Applications Using Node.js
/Learn Computer Science With JavaScript: Part 4, Functions
/Learn Computer Science With JavaScript: Part 2, Conditionals
/Create Interactive Charts Using Plotly.js, Part 5: Pie and Gauge Charts
/Create Interactive Charts Using Plotly.js, Part 4: Bubble and Dot Charts
Create Interactive Charts Using Plotly.js, Part 3: Bar Charts
/Awesome JavaScript Libraries and Frameworks You Should Know About
/Create Interactive Charts Using Plotly.js, Part 2: Line Charts
/Bulk Import a CSV File Into MongoDB Using Mongoose With Node.js
/Build a To-Do API With Node, Express, and MongoDB
/Getting Started With End-to-End Testing in Angular Using Protractor
/TypeScript for Beginners, Part 4: Classes
/Object-Oriented Programming With JavaScript
/10 Best Affiliate WooCommerce Plugins Compared
/Stateful vs. Stateless Functional Components in React
/Make Your JavaScript Code Robust With Flow
/Build a To-Do API With Node and Restify
/Testing Components in Angular Using Jasmine: Part 2, Services
/Testing Components in Angular Using Jasmine: Part 1
/Creating a Blogging App Using React, Part 6: Tags
/React Crash Course for Beginners, Part 3
/React Crash Course for Beginners, Part 2
/React Crash Course for Beginners, Part 1
/Set Up a React Environment, Part 4
1 /Set Up a React Environment, Part 3
/New Course: Get Started With Phoenix
/Set Up a React Environment, Part 2
/Set Up a React Environment, Part 1
/Command Line Basics and Useful Tricks With the Terminal
/How to Create a Real-Time Feed Using Phoenix and React
/Build a React App With a Laravel Back End: Part 2, React
/Build a React App With a Laravel RESTful Back End: Part 1, Laravel 9 API
/Creating a Blogging App Using React, Part 5: Profile Page
/Pagination in CodeIgniter: The Complete Guide
/JavaScript-Based Animations Using Anime.js, Part 4: Callbacks, Easings, and SVG
/JavaScript-Based Animations Using Anime.js, Part 3: Values, Timeline, and Playback
/Learn to Code With JavaScript: Part 1, The Basics
/10 Elegant CSS Pricing Tables for Your Latest Web Project
/Getting Started With the Flux Architecture in React
/Getting Started With Matter.js: The Composites and Composite Modules
Getting Started With Matter.js: The Engine and World Modules
/10 More Popular HTML5 Projects for You to Use and Study
/Understand the Basics of Laravel Middleware
/Iterating Fast With Django & Heroku
/Creating a Blogging App Using React, Part 4: Update & Delete Posts
/Creating a jQuery Plugin for Long Shadow Design
/How to Register & Use Laravel Service Providers
2 /Unit Testing in React: Shallow vs. Static Testing
/Creating a Blogging App Using React, Part 3: Add & Display Post
/Creating a Blogging App Using React, Part 2: User Sign-Up
20 /Creating a Blogging App Using React, Part 1: User Sign-In
/Creating a Grocery List Manager Using Angular, Part 2: Managing Items
/9 Elegant CSS Pricing Tables for Your Latest Web Project
/Dynamic Page Templates in WordPress, Part 3
/Angular vs. React: 7 Key Features Compared
/Creating a Grocery List Manager Using Angular, Part 1: Add & Display Items
New eBooks Available for Subscribers in June 2017
/Create Interactive Charts Using Plotly.js, Part 1: Getting Started
/The 5 Best IDEs for WordPress Development (And Why)
/33 Popular WordPress User Interface Elements
/New Course: How to Hack Your Own App
/How to Install Yii on Windows or a Mac
/What Is a JavaScript Operator?
/How to Register and Use Laravel Service Providers
/
waly Good blog post. I absolutely love this…