How to: Creating your own custom block in Magento

Posted by Stuart Forster on 26th August 2011

Blog - Magento Block

This is a quick 6 step tutorial on how to create a custom block in Magento.

Step 1: Global Module Configuration

Create your custom block configuration xml file inside the app/etc/modules/ directory. I named mine Gravitywell_All.xml.

Step 2: Directories

Create the correct directories for your module:

app/code/local/Gravitywell
app/code/local/Gravitywell/Example
app/code/local/Gravitywell/Example/Block
app/code/local/Gravitywell/Example/etc

 Step 3: Module Configuration

Add your module configuration file. This should be named config.xml and placed inside app/code/local/Gravitywell/Example/etc.

Step 4: Create the Block Class

Now you can create your block class. In this example we'll called the block simply Menu.php and place it inside app/code/local/Gravitywell/Example/Block.

class Gravitywell_Example_Block_Menu extends Mage_Core_Block_Template 
{

}

Step 5: Create the view script

Create a view script template file to use for your block. I'm simply going to place this as a file called menu.phtml inside the folder app/design/frontend/gravitywell/example/template/gravitywell.

Step 6: Embed the block inside your layout.xml or programmatically.