简体   繁体   中英

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?

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>

I'd like to add to fantasticrice's a answer. If you are calling this within an XML file. You only need to reference the HomePage CMS page.. You can do this by using the handle <cms_index_index> which is the homepage.

<!-- Homepage -->
<cms_index_index>
    <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>
</cms_index_index> 

You should define your module on app/etc/modules , if the module its created you should see in admin site (on web) configuration > advance , and check if the module its activated

<?xml version="1.0"?>
<config>
<modules>
    <your_module>   <!-- Name of Module -->
        <active>true</active>  <!-- This says if the module is active or not -->
        <codePool>local</codePool> <!-- This says the location of the module i.e inside the local folder. It can also be community folder. -->
    </your_module>
</modules>

And remember that your own implementations should be located on the local codepool , core codepool its for magento dev. of course you can extend the functionallity in local

My solution

I replaced from file: app/design/frontend/[mytheme]/default/layout/local.xml

this:

<block type="core/text_list" name="customblock" as="customblock" translate="label">
     <label>Custom Block</label>
</block>

to:

<block type="core/text_list" name="customblockreference" translate="label">
     <label>Custom Block</label>
</block>

so, now it is:

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

And <?php echo $this->getChildHtml('customblock') ?> from the page homepage.phtml become <?php echo $this->getChildHtml('customblockreference') ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM