Wednesday , December 19 2018
Home >> All tutorial >> Magento Developers’ Guides – Part 3: Create your own block

Magento Developers’ Guides – Part 3: Create your own block

Why create a block

For now, your plugin is a bit « simple », you can perform treatments but its usefulness and quite limited. We will now learn how to display content using a template, that’s what is a magento block ;)

How does it work? ok we’ll do a little reminder:

controller , call your layout to know what to show in this layout, you place blocks which are in fact a kind of « little controllers » (who will retrieve a list of client for exemple). Then these blocks appeals templates to display their informations.

Now that you understand how it works, the only way to understand it si to make one ! So…let’s go!

Step 1: Call the layout in the controller

Remind , your plugin is located in /app/code/local/Pfay/Test/. The plugin name is Test and it is in the namespace Pfay

Now open your controller:


And modify it like this:

  1. class Pfay_Test_IndexController extends Mage_Core_Controller_Front_Action
  2. {
  3.      public function indexAction()
  4.      {
  5.           $this->loadLayout();
  6.           $this->renderLayout();
  7.      }
  8.       public function mamethodeAction()
  9.       {
  10.           echo test mamethode’;
  11.       }
  12. }

As you can see, I added in my action index , two small lines. These lines will search what to display when you get on this action (by going to the URL for example).

  1. $this->loadLayout();    //Va chercher les elements à afficher
  2. $this->renderLayout();   //Affiche les elements

For now, there’s nothing more to do on the controller. We indicated to our controller that it must get the information in the layout, let’s see what happens on the side of the layout …

Step 2: Declare the items to be displayed using the Layout.

To start before you can use the Layout, (and if you read the previous tutorials you begin to predict the continued …) it must be declare, with a declaration Magento is going to be able to use it. To do this go into our file config.xml (located in the directory /etc of your module).

Open it and add it in <frontend> and after <routers> :

  1. <layout>
  2.    <updates>
  3.         <test>
  4.              <file>test.xml</file>
  5.          </test>
  6.     </updates>
  7. </layout>

This litte xml code will allow you to declare your file test.xml as the layout of your plugin test (because it is in your config.xml of your Test plugin)

Now declare the folder or will find the Blocks:

In <config> and bitter <frontend> now add:

  1. <global>
  2.      <blocks>
  3.          <test>
  4.               <class>Pfay_Test_Block</class>
  5.          </test>
  6.       </blocks>
  7. </global>

In you declare your Blocks, it is also where you will declare later your models and Helpers (we will see later what it is).

Important Note:

In Magento when you declare your classes, the name of your class is the path to this file, so our controller Pfay_Test_IndexControlleris at the following address: /app/code/local/Pfay/Test/IndexController.php

So you deduce that the names of your blocks will be the type Pfay_Test_IndexController_MonBlock

Now that the config.xml file is configured and magento is able to find our layout and our blocks, let’s see what happens on the side of the Layout!


If you recall, in the first lesson of this tutorial , we saw that a plugin contains some code template and that files are has two locations in the directories magento.

The layout is part of the « template », we declared it in our config.xml module as named test.xml

Now go to the directory layout of your Magento theme.

In our example, the template currently used in magento will be in the following directory: \app\design\frontend\pfay\theme\ .

We will therefore create a Layout test.xml in the directory \app\design\frontend\pfay\theme\layout\ 
Once this file is created, edit it and copy it:

  1. <layout version=“0.1.0”>
  2.      <default>
  3.           <reference name=“content”>
  4.           </reference>
  5.       </default>
  6.       <routeurfrontend_index_index>
  7.            <reference name=“content”>
  8.                 <block type=“test/monblock”  name=“afficher_monbloc”
  9.                           template=“test/afficher.phtml” />
  10.            </reference>
  11.       </routeurfrontend_index_index>
  12. </layout>

I do not tell you how it works, if you do not understand what code I advise you to read the guide magento designer and it must be mastered before starting to try to develop a module.
I’m going to remind you what are some piece of code in case;)

  1. <routeurfrontend_index_index>
  3. </routeurfrontend_index_index>

To indicate which blocks a magento add to wich page, we have created the layout!

This layout define the pages, in this pages we add blocks. One page is written like this: «  router name »_ »name of the controller »_ »action name » 
So if you want to access our action we have already defined in the config.xml that the name of the router will be routeurfrontend , we know that the controller called in this case is the controller IndexController.php , and that the method wich will be called is indexAction() . The page will be in the layout represented by the tag: routeurfrontend_index_index

And we will add in this file, the line:

  1. <block type=“test/monblock” name=“afficher_monbloc” template=“test/afficher.phtml” />

That mean for magento « creates a block that you will take in the Test module, which is called « monblock » and use the template « afficher.phtml » which will be in the folder « test » of the « template » folder.

Step 3: Create the block

Now, we have to create the block «  monblock « ,so go to the folder you specified in config.xml to store blocks (app/code/local/Pfay/Test/Block) and create the Monblock.php file in it.

This file will contain:

  1. <?php
  2. class Pfay_Test_Block_Monblock extends Mage_Core_Block_Template
  3. {
  4.      public function methodblock()
  5.      {
  6.          return informations about my block !!’ ;
  7.      }
  8. }

This file is just a class of type Block (Mage_Core_Block_Template) with a method methodblock() which returns a sentence (String)

Step 4: Create the template

Now go to the directory \app\design\frontend\pfay\theme\template\

And create a directory « test » with a file inside afficher.phtml.

This file will contain:


echo $this->methodblock();


How it works

We have defined a block of type « test/monblock » in the layout, which appeals to our template afficher.phtml. So in afficher.phtml we can access to the monblock functions thanks to the variable $this.

Step 5: Test!

Now go to and now, you can see appear on your website a text: « information from my block!  »

End of this tutorial

Now you can insert a block with the template in any page of your site. Our example is simple because the goal is to understand how Magento work. Our block is not really usefull in a real website.

A block is generally used to make treatment of the style list the last 5 products added to your Magento store, display a contact form… you would be happy to hand them over several locations your site just by inserting a small line in your layout is not it ? Ok now you understand What does a block and how to create one, Congratulations ! ;)

Here is the end of this tutorial, i hope you enjoyed this, feel free to leave comments if you have any questions or comments ;) You can help me by sharing this tutorial on facebook, twitter or, talking about this blog with friends. Read more about Magento Blocks here.

Here is the library of Magento Download for FREE (all versions)

Your Magento website needs a powerful Magento ERP for stock control.


magento erp

About Champ

Leave a Reply

Your email address will not be published. Required fields are marked *

Heap | Mobile and Web Analytics