Magento: create a new block and add it to a template

1. Module installation xml file, MageRoot/app/etc/modules/MyExtensions_HelloBlock.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <MyExtensions_HelloBlock>
            <active>true</active>
            <codePool>local</codePool>
        </MyExtensions_HelloBlock>
    </modules>
</config>

2. Module configuration xml file, MageRoot/app/code/local/MyExtensions/HelloBlock/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <MyExtensions_HelloBlock>
            <version>0.0.1</version>
        </MyExtensions_HelloBlock>
    </modules>
    <global>
        <blocks>
            <helloblock>
                <class>MyExtensions_HelloBlock_Block</class>
            </helloblock>
        </blocks> 
    </global>
</config>

3. Block class, MageRoot/app/code/local/MyExtensions/HelloBlock/Hello.php

<?php
class MyExtensions_HelloBlock_Block_Hello extends Mage_Core_Block_Template 
{    
    public function hello()
    {
    	echo "hello";
    }
}
?>

4. Template file for the block, MageRoot/design/frontend/default/default/template/helloblock/hello.phtml

<?php
$this->hello();
?>

To create this block dynamically in any existing template file

//helloblock matches <helloblock> in the module config file
//hello matches the block class name
echo $this->getLayout()->createBlock('helloblock/hello')->setTemplate('helloblock/hello.phtml')->toHtml();

To insert this block by adding it to a existing layout file

<block type="helloblock/hello" name="helloblock" template="helloblock/hello.phtml"/>

For example, to insert this to the catagory product view xml, MageRoot/app/design/frontend/base/default/layout/catalog.xml

   <catalog_product_view translate="label">
        <label>Catalog Product View (Any)</label>
        <!-- Mage_Catalog -->
        <reference name="root">
            <action method="setTemplate"><template>page/2columns-right.phtml</template></action>
        </reference>
        <reference name="head">
            <action method="addJs"><script>varien/product.js</script></action>
            <action method="addJs"><script>varien/configurable.js</script></action>

            <action method="addItem"><type>js_css</type><name>calendar/calendar-win2k-1.css</name><params/><!--<if/><condition>can_load_calendar_js</condition>--></action>
            <action method="addItem"><type>js</type><name>calendar/calendar.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>--></action>
            <action method="addItem"><type>js</type><name>calendar/calendar-setup.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>--></action>
        </reference>
        <reference name="content">
        	<!--This is the line added to this catalog.xml file-->
            <block type="helloblock/hello" name="helloblock_hello" template="helloblock/hello.phtml"/>

After the hello block is added, refresh the cache and you will see the hello when you are on a product detail page.

Add a block to the before_body_end block in Magento
Magento: Passing variables among controller, block, hepler, model and template
Call a block directly without define it in the layout file in Magento
Adding new css js files to magento’s template
Pass a variable to a template .phtml block in Magento

Search within Codexpedia

Custom Search

Search the entire web

Custom Search