开发者

Magento Block Injction via module.xml Files

In a module I am writing I want to use using mymodule.xml to insert my own block after this block:

<block type="catalog/product_view_media" name="product.info.media" as="media" template="catalog/product/view/media.phtml"/>

which is nested within

<reference name="content">
    <block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">

as can be seen in app/design/frontend/base/default/layout/catalog.xml

I have tried many variants such as:

<reference name="content">
   <reference name="product.info">
      <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
   </reference>
</reference>

and just

<reference name="product.info">
   <开发者_Go百科block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
</reference>

and

<reference name="product.info">
   <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
</reference>

but I cant seem to find the correct combination. the only one that renders my block is:

<reference name="content">
   <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
</reference>

but obviously it is not placed where I wanted it to be placed.


If you look at the declaration of the content block up in page.xml, you'll see the following.

<block type="core/text_list" name="content" as="content" translate="label">

By default, the block named content is a core/text_list, which translates to a Mage_Core_Block_Text_List.

The purpose of a core/text_list blocks is simple. They automatically render any blocks inserted into them. That's why you can insert a block into content successfully.

The block you want to insert into

<block type="catalog/product_view" name="product.info" template="mymodule/folder/class.phtml" ...

is a catalog/product_view, which translates to a Mage_Catalog_Block_Product_View, which ultimately inherits from Mage_Core_Block_Template. That makes it a Template block. Template blocks do not automatically render their children. Template blocks will render a phtml file. If that phtml contains a call to

$this->getChildHtml('block_name');

then the block with that specific name will be rendered. If the phtml file contains a call to

$this->getChildHtml(); //no arguments

then all the child blocks will be rendered.

So, when you say

<reference name="product.info">
   <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
</reference>

you're inserting a block of type mymodule/folder_class into the product.info block. But, because product.info is a template block and not a text list block, it doesn't render the block you inserted. You'll need to add a custom catalog/product/view.phtml template to your theme (by copying the base template), and then at the bottom of view.phtml, add

<?php echo $this->getChildHtml('mymodule.folder.class');?>


If you want to add the block in the catalog/product/view action, I believe you should put it like this, else replace catalog_product_view by the desired action or default for any actions.

<layout version="0.1.0">
    <catalog_product_view>
        <reference name="content">
            <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class">
            </block>
        </reference>
    </catalog_product_view>
</layout>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜