我创建了自己的模块将相关产品添加到产品页面,这仅显示具有相同品牌/制造商的相关产品。
但是,我遇到了模板文件无法显示在页面上的问题。
这是我到目前为止所拥有的。
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();
}
}
?>
然后在目录.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
没有显示,我只是不知道为什么。有谁知道我可能错过了什么?
这个
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
.