简体   繁体   中英

Magento custom block template not showing

I've created my own module to add related products to the product page, this only shows related products with the same brand/manufacturer.

However I've come across an issue where the template file won't show on the page.

Here's what I have so far.

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();
    }
}
?>

Then in the catalog.xml file I've added the following in the catalog_product_view area:

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

Then within design/frontend/MYPACKAGE/default/relatedbrand/view.phtml I have:

<?php echo 'HELLO'; ?>

Also within catalog/product/view.phtml I've added:

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

When I navigate to a product page I can see Block's _toHtml() method called! however HELLO isn't shown and I just can't figure out why. Does anyone have any idea what I may have missed?

This

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

Should be:

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

_toHtml method from the Mage_Core_Block_Template does not echo the content. It just returns it. In your case the method returns null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM