【问题标题】:Add custom block in Magento在 Magento 中添加自定义块
【发布时间】:2015-05-11 16:29:04
【问题描述】:

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

在主页.phtml中

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

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

<h1>test</h1>

我哪里做错了?

【问题讨论】:

  • 您必须将通过homepage.phtml 显示的块的名称放在参考节点的名称属性中。但是由于 homepage.phtml 不是来自 Magento 的股票,如果您至少不向我们提供声明此父块的布局,我们将无能为力。

标签: php magento


【解决方案1】:

我假设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>

【讨论】:

  • 我尝试将块名称从 customblock 更改为 customblockreference 就像参考名称一样,它可以工作,块显示在家里,但我不知道这是不是更好的方法来做到这一点
【解决方案2】:

我想在fantasyrice 中添加一个答案。如果您在 XML 文件中调用它。您只需要引用 HomePage CMS 页面。您可以使用句柄 &lt;cms_index_index&gt; 这是主页。

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

【讨论】:

  • 但是如果将&lt;default&gt;替换为&lt;cms_index_index&gt;,则只能在首页添加块,不能在模板的其他部分添加,不是吗?
  • 正确。 cms 索引仅是主页 cms
【解决方案3】:

您应该在 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 开发。当然你可以在本地扩展功能

【讨论】:

  • 对不起,你错了。 local.xml 是一个布局文件,可以在 Magento 的任何主题中工作,无需声明。 source
【解决方案4】:

我的解决方案

我从文件中替换: 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>

&lt;?php echo $this-&gt;getChildHtml('customblock') ?&gt;从页面homepage.phtml变成&lt;?php echo $this-&gt;getChildHtml('customblockreference') ?&gt;

【讨论】:

  • 这与我的解决方案相同,只是您使用了不同的名称。另外,我仍然会在 customblockreference 中直接定义 customblock,因为当您在同一个文件中定义两个块时,没有理由使用 reference
  • 你是对的,你的解决方案更容易。所以你已经说过这部分&lt;reference name="customblockreference"&gt; &lt;block type="core/template" name="customblock" template="customblock.phtml" /&gt; &lt;/reference&gt;只有当我需要在其他.xml文件中的customblockreference中添加一个块时才有用,对吧?
  • 是的,这就是reference的目的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-15
  • 1970-01-01
相关资源
最近更新 更多