【问题标题】:Magento custom block template not showingMagento 自定义块模板未显示
【发布时间】:2013-10-10 08:29:14
【问题描述】:

我已经创建了自己的模块来将相关产品添加到产品页面,这仅显示具有相同品牌/制造商的相关产品。

但是我遇到了模板文件不会显示在页面上的问题。

这是我目前所拥有的。

app/code/community/CustomMod/RelatedBrand/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config> 
    <modules>
        <CustomMod_RelatedBrand>
            <version>0.0.1</version>
        </CustomMod_RelatedBrand>
    </modules>
    <global>
        <blocks>
            <relatedbrand>
                <class>CustomMod_RelatedBrand_Block</class>
            </relatedbrand>
        </blocks>
    </global>
</config>

app/code/community/CustomMod/RelatedBrand/Block/Related.php

<?php
class CustomMod_RelatedBrand_Block_Related extends Mage_Catalog_Block_Product_View {    
    public function _toHtml() {
        echo "Block's _toHtml() method called!";
        parent::_toHtml();
    }
}
?>

然后在 catalog.xml 文件中,我在 catalog_product_view 区域添加了以下内容:

<block type="relatedbrand/related" name="related_brand" as="related_brand" template="relatedbrand/view.phtml"/>

然后在 design/frontend/MYPACKAGE/default/relatedbrand/view.phtml 我有:

<?php echo 'HELLO'; ?>

在我添加的 catalog/product/view.phtml 中:

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

当我导航到产品页面时,我可以看到Block's _toHtml() method called!,但是没有显示HELLO,我就是不知道为什么。有谁知道我可能错过了什么?

【问题讨论】:

    标签: php magento


    【解决方案1】:

    这个

    public function _toHtml() {
        echo "Block's _toHtml() method called!";
        parent::_toHtml();
    }
    

    应该是:

    public function _toHtml() {
        echo "Block's _toHtml() method called!";
        return parent::_toHtml();
    }
    

    Mage_Core_Block_Template 中的_toHtml 方法不回显内容。它只是返回它。在您的情况下,该方法返回null

    【讨论】:

    • 哇,就是这样。非常感谢,最后一个小时一直在扯我的头发!我现在觉得有点傻:P
    • @Karl 不客气。并且不要觉得有点愚蠢——即使我会:)。这样的事情会发生。有人告诉我“只有白痴才害怕寻求帮助”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    相关资源
    最近更新 更多