Php 在Magento中添加自定义块

Php 在Magento中添加自定义块,php,magento,Php,Magento,在Magento 1.9中,我想在主页中添加一个自定义块,但什么也没发生。我有以下文件: app/design/frontend/[mytheme]/default/layout/local.xml <?xml version="1.0" encoding="UTF-8"?> <layout> <default> <reference name="root"> <block type="core

在Magento 1.9中,我想在主页中添加一个自定义块,但什么也没发生。我有以下文件:

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>

自定义块
在homepage.phtml中

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

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

<h1>test</h1>
测试

我哪里做错了?

你应该在app/etc/modules上定义你的模块,如果模块是创建的,你应该在admin site(on web)configuration>advance中查看,并检查模块是否已激活

<?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>

真的
地方的


请记住,您自己的实现应该位于本地代码池中,即magento dev的核心代码池。当然,您可以扩展本地中的功能。我假设
主页。phtml
是您用于主页的根模板,如果不是这样,请澄清

我认为问题在于
core/text\u列表
block
customblock
正在您的根模板
homepage.phtml中呈现,但您尚未向该块添加任何内容。
core/text\u列表
只是一个容器,用于呈现添加到其中的子块

您可能试图将
customblock.phtml
添加到新的
core/text\u列表
,如果是这种情况,应该是这样的:

<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>
<reference name="customblock">
    <block type="core/template" name="customblock-child" template="customblock.phtml"/>
</reference>

我想补充一点,莱斯的回答。如果您在XML文件中调用它。您只需参考主页CMS页面。。您可以使用主页上的句柄
来执行此操作

<!-- 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> 

自定义块

我的解决方案

我从文件中替换了:
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="customblockreference" translate="label">
              <label>Custom Block</label>
         </block>
    </reference>
    <reference name="customblockreference">
        <block type="core/template" name="customblock" template="customblock.phtml" />
    </reference>
</default>

自定义块


和主页上的
。phtml变成

对不起,你错了
local.xml
是一个布局文件,可以在Magento的任何主题中工作,而无需声明。您必须将通过
homepage.phtml
显示的块的名称放入引用节点的name属性中。但是由于
homepage.phtml
不是来自股票Magento,如果您不至少向我们提供声明父块的布局,我们就无能为力。我尝试将块名从
customblock
更改为
customblockreference
,就像参考名一样,它可以工作,块显示在主页中,但我不知道这是否是更好的方法。这与我的解决方案相同,只是您使用了不同的名称。此外,我仍然会直接在
customblockreference
中定义
customblock
,因为在同一文件中定义这两个块时,没有理由使用
reference
。那么您是如何说这部分
只有在我需要在
customblockreference
的其他.xml文件中添加块时才有用的,对吗?是的,这就是
引用的目的,但是如果用
替换
,块只能添加到主页中,而不能添加到模板的其他部分,是吗?没错。Cms索引仅限于主页Cms
<block type="core/text_list" name="customblockreference" translate="label">
     <label>Custom Block</label>
</block>
<?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>