在 Magento 中添加自定义块

Add custom block in Magento

在 Magento 1.9 中,我想向主页添加一个自定义块,但没有任何反应。我有这些文件:

app/design/frontend/[我的主题]/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 上定义您的模块,如果模块是创建的,您应该在管理站点(网络)配置 > 高级中看到它,并检查模块是否已激活

<?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 的。当然你可以在 local

中扩展功能

我假设 homepage.phtml 是您用于主页的根模板,所以如果不是这样请澄清。

我认为问题在于 core/text_listcustomblock 正在您的根模板 homepage.phtml 中呈现,但您尚未向该块添加任何内容。 core/text_list 只是一个容器,它呈现添加到其中的子块。

看起来您可能正在尝试将 customblock.phtml 添加到新的 core/text_list,如果是这样的话,它应该是这样的:

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

这会将子模板块直接添加到 core/text_list,因为您只是在同一个文件中定义两者。但是,如果您需要从其他地方向 core/text_list 添加一个新块,您可以这样做:

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

我想补充一下 fantasticrice 的答案。如果您在 XML 文件中调用它。您只需要引用 HomePage CMS 页面。您可以使用主页句柄 <cms_index_index> 来完成此操作。

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

这个:

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

至:

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

并且<?php echo $this->getChildHtml('customblock') ?>从页面homepage.phtml变为<?php echo $this->getChildHtml('customblockreference') ?>