ingo
ingo

Reputation: 116

Magento: Referencing a custom block does not work

I'm trying to reference a block from an other custom module to add a child block via layout file but it does not work.

The first layout file contains

<catalog_product_view>
    <reference name="content">
        <block type="core/template" name="tabcontainer" as="tabcontainer"
            template="store/tabcontainer.phtml" >
            <block type="catalog/product_list_related" name="kitparts"
                template="store/product/kitparts.phtml"/>
        </block>
    </reference>
</catalog_product_view>

and in the second one I try to reference the tabcontainer block

<catalog_product_view>
    <reference name="tabcontainer">
        <block type="productshippinginfo/productshipping" name="productshippinginfo"
            template="productshippinginfo/productshipping.phtml" after="kitparts"/>
    </reference>
</catalog_product_view>

but the productshippinginfo block is not displayed while it is definitely included in the layout (using Alan Storm's layoutviewer plugin). If I reference content it is displayed.

What is wrong? Isn't it possible to add a child to a custom block from a custom extension?

Thanks for your help!

(I'm using Magento 1.6.1.0)

[edit] in tabcontainer.phtml I'm calling <?php echo $this->getChildHtml(); ?>

Upvotes: 2

Views: 3440

Answers (3)

ingo
ingo

Reputation: 116

First of all: Thank you Vinai!

Adding a dependency to control the loading order of my plugins it works!

in File: app/etc/modules/Company_ContentModule.xml

<Company_ContentModule>
  <active>true</active>
  <codePool>local</codePool>
  <depends>
    <Company_ContainerModule />
  </depends>
</Company_ContentModule>

So the content module is loaded after the container module.

Upvotes: 4

benmarks
benmarks

Reputation: 23205

You are close. You just need to add this to you store/tabcontainer.phtml file:

getChildHtml('productshippinginfo'); ?>

The reason blocks that are children of "content" render without a template change is that the "content" block is a core/text_list block. If you look in Mage_Core_Block_Text_List, you will see that in its rendering method (_toHtml()) it renders its children.

You could also add an empty getChildHtml() call to your tabcontainer template to achieve a similar effect as a core/text_list - in fact, if you use getChildHtml('',false,true); you'll get the sorted children (set with before="" and after="" params).


EDIT: adjusted the getChildHtml() call syntax based on OP's comment correct findings that the first param must be an empty string a/o/t a boolean.

Upvotes: 1

Nasaralla
Nasaralla

Reputation: 1839

In the second layout I think you need to provide the nesting:

<catalog_product_view>
<reference name="content">
<reference name="tabcontainer">
    <block type="productshippinginfo/productshipping" name="productshippinginfo"
        template="productshippinginfo/productshipping.phtml" after="kitparts"/>
</reference>
</reference>
</catalog_product_view>

In order that maganto picks that up

And because you are doing

<?php echo $this->getChildHtml(); ?>

You do not need to specifically call it by name unless you want it to appear in a particular place in your HTML output.

In order to test if your block is appearing in the page at all add output="toHtml" in you block tag.

<block type="productshippinginfo/productshipping" name="productshippinginfo"
    template="productshippinginfo/productshipping.phtml" after="kitparts" output="toHtml"/>

Upvotes: -1

Related Questions