Add a block to the before_body_end block in Magento

When we want to add a javascript plugin which require to be added just before the end of html body tag, we can add a block in the before_body_end block in Magento. The steps below are a example of how to add a new block to the before_body_end block in Magento.

1. Create the Module installation xml file to let Magento know the new Module.
MyExtensions_AddBlock.xml

<?xml version="1.0"?>
<config>
    <modules>
        <MyExtensions_AddBlock>
            <active>true</active>
            <codePool>local</codePool>
        </MyExtensions_AddBlock>
    </modules>
</config>

2. Create the module config xml file to specify the module version, block class and block layout file for the task.
app/code/local/MyExtensions/AddBlock/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <MyExtensions_AddBlock>
            <version>0.1.0</version>
        </MyExtensions_AddBlock>
    </modules>
    <global>
        <blocks>
            <addblock>
                <class>MyExtensions_AddBlock_Block</class>
            </addblock>
        </blocks>
    </global>
    <frontend>
        <layout>
            <updates>
                <addblock>
                    <file>addblock.xml</file>
                </addblock>
            </updates>
        </layout>
    </frontend>
</config>

3. Create the layout file for the block, in this case, it is adding the block to the before_body_end block. So, whenever the before_body_end block is called, the block we are creating here will show up.
app/design/frontend/default/default/layout/addblock.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="before_body_end">
            <block type="addblock/myblock" name="ab.myblock" template="addblock/myblock.phtml"/>
        </reference>
    </default>
</layout>

4. Create the template file for our block, this is template that will show up just before the body tag on the html page.
app/design/frontend/default/default/template/addblock/myblock.phtml

<p>Hello this is my block generatedfrom app/design/frontend/default/default/template/addblock/myblock.phtml 
you can put php and javascript code in this template as well</p>
<?php $this->hello();?>
<script type="text/javascript">
alert("hello");
</script>

5. Create the block class, whenever the above template file is rendered, it is an object of this block class and it can use any functions defined in this class.
app/code/local/MyExtensions/AddBlock/Block/MyBlock.php

<?php
class MyExtensions_AddBlock_Block_MyBlock extends Mage_Core_Block_Template
{
    public function hello()
    {
        echo "Hello, this is my block generated from the function hello() in app/code/local/MyExtensions/AddBlock/Block/MyBlock.php!";
    }
}

6. With above code, the block will render whenever the before_body_end block is called, which is usually called just before the end of html body tag by default.
We can also directly call the block we just created.

<?php echo $this->getLayout()->createBlock('addblock/myblock')->setTemplate('addblock/myblock.phtml')->toHtml(); ?>

Search within Codexpedia

Custom Search

Search the entire web

Custom Search