Custom Layouts and Templates With Magento

In the first part of this series, we learned the basics of Magento module development including the Magento directory structure, the custom module structure, and we created a basic "Hello World" module in order to understand how controllers work in Magento. 

In this article, we will learn how to create blocks and layouts files. Specifically, we'll look at how layout files and blocks files work in Magento and we'll look at the rendering of layout files. 

Looking for a Quick Solution?

If you're looking for a quick solution, there's a great collection of Magento themes and templates over at Envato Market. It's a great way to quickly build up a collection of high-quality low poly items for your project.

You can also get personalised help by hiring one of the Magento service providers on Envato Studio.

Magento service providers on Envato Studio

But, on with the tutorial! First, we will see what the layout and block files are and how they are useful in rendering front-end pages in Magento, and then we will see how we can include them in our custom module.

What Is the Layout File?

As the name suggests, layout files are useful in rendering front pages of Magento. Layout files are XML files that reside in in app > design > frontend > your interface > your theme > layoutHere, you can see that there are many layout files for any given module. Each Magento module has its own layout files much like the customer module has the customer.xml layout file, catalog module have catalog.xml layout file etc. These layout files contain structural blocks and content blocks

If you're wondering why Magento needs these blocks, you can learn more about this in the first part of the series.

Let's Dig More Into Layout Files

Let's dig more into layout files by looking at one example. Go to app > design > frontend > base > layout and open the customer.xml file. Here, all blocks are wrapped around main <layout> tag. You can see the different <tag> which contains specific blocks. 

See the below snippet:

Handle

Handles are the main entity through which Magento identifies which block to load when a  particular module is being called. <customer_account_create> is the module-specific handle. This handle is being triggered when someone opens the customer registration page. 

Each handles content-nested block specific to the page. Some of layouts files contain the <default> handle. At this stage, you may ask about the difference between module specific-handles and default handles. In short, module-specific handles only render the blocks inside it when that module is being rendered in browser whereas default handle loads in most of the page.

<block>

Inside the handle there is the different different blocks which specify the template file to be render when that block is being called. Blocks are of two types: 

  1. Structurer Blocks
  2. Content Blocks

In our layout file we are defining only the Content Blocks and then we wrap them in Structurer BlocksFor example, if someone is calling a customer registration page and we want to load it on left, right, content, or footer, we wrap that block in its respective structurer block. Here, we have wrapped two blocks inside the "content" block which is a structurer block. 

Blocks contains attributes like following:

  1. type defines the block classes in which we can define the different functionality
  2. name defines the unique name of particular block so the other blocks can make a reference of existing block by name and extend it
  3. before/after are attributes that we can set that allow us to define the position of our block inside structurer block
  4. template defines the actual phtml file name and path where our HTML and PHP code goes
  5. action allows us to trigger any action like loading JavaScript and all by using this attribute.
  6. as is an attribute is mainly used for structurer blocks

<reference>

The <reference> tag is use to extend the blocks that already exist. In this case, we have extended the content block and inserted our own block into it. You must use the correct block name that you want to extend.

<remove>

The <remove> tag is use to remove the specific block. For example, say that on your account registration page you, don't want to display the right and left columns. In that case, then you can simply remove that block with the following syntax <remove name="your block name">.

Child <block>

When you wrap one block under another block, that wrapped block is called child block. Whenever our parent block is called by our module, the child block is automatically called.

You can call child block separately too with below syntax in you template file
echo $this->getChildHtml(‘child');

root <block>

Open page.xml layout file, you will find the <root> block that is looks like below

Magento starts rendering from the root block. All other blocks are child blocks of the root block. The root block defines the structure of the page. Here, you can see that currently it's set to 3columns.phtml, you can change this to 1column.phtml, 2columns-right.phtml or 2columns-left.phtml.

Adding CSS and JavaScript to the Layout XML

For any particular page you can add CSS and JavaScript files into layout tag like this:

Here you can see that we have added a CSS file and a JavaScript file inside the head of our customer account page.

What Are The Block Classes?

Block classes are use to define the functions that are specific to particular block. Block classes file are resides in app > code > local/community/core > your module namespace > your module name > Block directory. These file contains the functions which we can use directly with $this keyword in block specific template file. Let's take one example to understand the block classes.

Go to review.xml file that is resides in app > design > frontend > base > default > layout directory and find the below line of code:

Here you can see the block review/customer_recent that refer to the template recent.phtmlGo to app > design > frontend > base > default > template > review > customer and open recent.phtml

In this file, you can see two functions being called using $this keyword. They are $this->getCollection() and $this->count()These functions are defined in their block classes file recent.php  that resides in app > code > core > Mage > review > Block > customer directory.

Here, the block type = "review/customer_recent" refers to the  Mage_Review_Block_Customer_Recent block class that is define in recent.php file. Whatever functions you define in this class, you can directly use it in corresponding template file with $this.

Creating Custom Module Layout and Block Files

Lastly, we were left with custom "Hello World" module with controller. Here, we create the layout file of our custom module.so let's create it.

To create the layout file, we need to first create the block class file. Before we add the class file, we need to tell our module that we are including the block files. So go to app > code > local > Chiragdodia > Mymodule > etc > config.xml and add the below line of code:

The final XML file contains the following lines of code:

Create the Block Class File

Next, go to app > code > local > Chiragdodia > Mymodule > Block and create the file Mymodule.php that contains below lines of code

Here we have declared the class Chiragdodia_Mymodule_Block_Mymodule that contains the function myfunction which we can call directly from our layout template file.

Create the Layout XML File

Go to app > design > frontend > default > default > layout and create the mymodule.xml file which contains below lines of code

Create the Template File

Go to app > design > frontend > default > default > template and create the mymodule.phtml file. In this file, we will call the function myfunction that we have declared in our block class.

If everything is correct thus far, you will see the output with three columns layout by accessing the URL yoursite.com/index.php/mymodule/index.

In some Magento versions, default themes do not contain the layout and template directories. In that case you can create your layout and template file in app > design > frontend > base directory.

This is how layouts work in Magento. In the previous article, we created the simple "Hello World" module, in this article we have created it using layout file. The Magento layout structure is little bit difficult to understand at first, but once you begin tinkering with it, you will easily get the idea behind it. 

In this post, I have attached the module demo files that we have created till now. Feel free to comment and ask any questions that you may have about this particular issue.

Tags:

Comments

Related Articles