So you just want to create your own custom block. Then follow these steps. I am going to call this module as Rkt_CustomBlock
.
First of all, these are the files that are avilable in this extension
rootdir
|________ app
|________ etc
| - Rkt_CustomBlock.xml
|________ code/local/Rkt/CustomBlock
| |________ etc
| | - config.xml
| |________ controllers
| | - IndexController.php
| |
| |________ Block
| - Helloworld.php
|
|________ design/frontend/rwd/default
|________ layout
| - rkt_customblock.xml
|________ template/rkt_customblock
- helloworld.phtml
First of you need to carefully construct this file structure. Now we can go into the codes.
Module Activation File
File : app\etc\modules/Rkt_CustomBlock.xml
<config>
<modules>
<Rkt_CustomBlock>
<active>true</active>
<codePool>local</codePool>
</Rkt_CustomBlock>
</modules>
</config>
This file is used to activate our custom module. Here it does two things. First it makes our module enabled (<active>true</active>
) and secondly defines where our module is going to reside. According to the definition, our module is going to reside in local
codepool.
Module Configuration File
file : app\code\local\Rkt/CustomBlock/etc/config.xml
<config>
<modules>
<Rkt_CustomBlock>
<version>1.0.0</version>
</Rkt_CustomBlock>
</modules>
<frontend>
<routers>
<rkt_customblock>
<use>standard</use>
<args>
<module>Rkt_CustomBlock</module>
<frontName>helloworld</frontName>
</args>
</rkt_customblock>
</routers>
<layout>
<updates>
<rkt_customblock>
<file>rkt_customblock.xml</file>
</rkt_customblock>
</updates>
</layout>
</frontend>
<global>
<blocks>
<rkt_customblock>
<class>Rkt_CustomBlock_Block</class>
</rkt_customblock>
</blocks>
</global>
</config>
This file is used to configure our module. <modules>
node is used to define version of our module. Here our module version is 1.0.0
This section is not important for us. Since we only deal with blocks. So this part is not necessary in our case. But it is a good practice to add this section in our module.
<frontend>
section is used to declare a unique router
and a frontend layout xml
file for our extension. Here we set router <frontName>
as helloworld
. This means any url which is going to follow www.yourdomain.com/helloworld/...
format is going to use our module. We declared a layout xml file for our extension only to add layout updates of our own custom extension.
<global>
section is used to declare our block section. The code inside this node two things here. First it set a unique identifier for extension's block. Here it is rkt_customblock
(in general namespace_modulename
). Beware.. it should be unique. Also it defines where exactly extension is going to hold block classes. According to the defintion, it will be at Rkt_CustomBlock_Block
. Magento parse this value to the location app\code\local/Rkt/CustomBlock/Block
to find our custom block classes.
Controller of our Extension
File : app\code\local\Rkt/CustomBlock/controllers/IndexController.php
<?php
class Rkt_CustomBlock_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
}
First our controller extends from the class Mage_Core_Controller_Front_Action
. This is very important. Every frontend controller classes should extend from this core class.
Now as you can see our controller holds only one method indexAction()
and it just loads the layout and then render those layout. Those two simple, humble lines of codes do a lot and I dont want to explain all those things here. You just want to know, those codes are responsible for constituting a magento page. Generally almost every controller action method hold these two line of codes.
Define Custom Block
File : app\code\local\Rkt/CustomBlock/Block/Helloworld.php
<?php
class Rkt_CustomBlock_Block_Helloworld extends Mage_Core_Block_Template
{
public function getText()
{
return "I am inside `" . get_class(). "` block and it is my first custom block ever !";
}
}
Helloworld
is our custom block. This block is going to show in frontend. You can see that our block extends from Mage_Core_Block_Template
. This is very important, because that class provides us a lot of template related functionalities.
Secondly, you can see that, I defined a custom method getText()
. The usage of this method will be discussed later.
Layout Update XML file
File : app\design\frontend\rwd\default\layout\rkt_customblock.xml
<layout>
<rkt_customblock_index_index>
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
<reference name="content">
<block type="rkt_customblock/helloworld" name="myblock.helloworld" template="rkt_customblock/helloworld.phtml" />
</reference>
</rkt_customblock_index_index>
</layout>
<rkt_customblock_index_index />
is known as layout handle. More specifically we can call this layout handle as controller action layout handle
. This is because, the layout definition inside this layouthandle will get included in the layout only when a specific controller action is get executed. In the above case, this layout handle will get processed only when indexAction()
method present in IndexController.php
of Rkt_CustomBlock
.
root
block is used to set a page template for the page. We included our custom block inside content
block. (root
and content
blocks are core blocks which are used in magento layout construction. These blocks has specific usages).
This is how our block is defined inside our module
<block type="rkt_customblock/helloworld" name="myblock.helloworld" template="rkt_customblock/helloworld.phtml" />
type
: this is used to recognize the block class. Here rkt_customblock/helloworld
stands for the block Rkt_CustomBlock_Block_Helloworld
.
name
: name of the block. It should be unique.
template
: this is the template file which is going to use by the block.
Define Template
File : app\design\frontend\rwd\default\template\rkt_customblock/helloworld.phtml
<h1>Hello World !</h1>
<p>Hello... Can you see me ? If yes I just want to say</p>
<h4><?php echo $this->getText(); ?></h4>
Here you can see that, we are calling to the method getText()
which is defined inside your custom block. This means that, helloworld.phtml
file is actually an instance of Rkt_CustomBlock_Block_Helloworld
.
Now, go and clear the cache and load the page www.yourdomain.com/helloworld
. You will find an output like this.
Hello World !
Hello... Can you see me ? If yes I just want to say
I am inside Rkt_CustomBlock_Block_Helloworld
block and it is my
first custom block ever !
Note 1: This module is based on Magento 1.9
and later. For all lower version, you can use base/default
package theme for your layout instead of rwd/default
.
You can quickly refer this small extension here. Let me know if you have any doubts !!
<routeurfrontend_index_index>
) route