Search code examples
phpmagentomagento-1.9

Add custom block in Magento


In Magento 1.9 I want to add a custom block to the homepage, but nothing happens. I have these files:

app/design/frontend/[mytheme]/default/layout/local.xml

<?xml version="1.0" encoding="UTF-8"?>
<layout>
    <default>
        <reference name="root">
            <block type="core/text_list" name="customblock" as="customblock" translate="label">
                <label>Custom Block</label>
            </block>
        </reference>
        <reference name="customblockreference">
            <block type="core/template" name="customblock" template="customblock.phtml" />
        </reference>
    </default>
</layout>

In homepage.phtml

 <?php echo $this->getChildHtml('customblock') ?>

in app/design/frontend/[mytheme]/default/template/customblock.phtml

<h1>test</h1>

Where am I doing wrong?


Solution

  • I am assuming that homepage.phtml is the root template you are using for the home page, so please clarify if that is not the case.

    I think the problem is that the core/text_list block customblock is getting rendered in your root template homepage.phtml but you have not added anything to that block. The core/text_list is just a container which renders child blocks that are added to it.

    It seems like you might be trying to add customblock.phtml to the new core/text_list, if that is the case it should be something like this:

    <reference name="root">
        <block type="core/text_list" name="customblock" translate="label">
            <label>Custom Block</label>
            <block type="core/template" name="customblock-child" template="customblock.phtml"/>
        </block>
    </reference>
    

    That will add the child template block directly to the core/text_list since you are just defining both here in the same file. However, if you needed to add a new block to that core/text_list from elsewhere, you could do it like this:

    <reference name="customblock">
        <block type="core/template" name="customblock-child" template="customblock.phtml"/>
    </reference>