Add a child block to product.info.tabs description tab
-
12-12-2019 - |
Question
I am using tabs for my product view page. In my local.xml I have the following:
<catalog_product_view>
<reference name="product.info">
<block type="catalog/product_view_tabs" name="product.info.tabs" as="info_tabs"
template="catalog/product/view/tabs.phtml">
<action method="addTab" translate="title" module="catalog">
<alias>description</alias>
<title>Description</title>
<block>catalog/product_view_description</block>
<template>catalog/product/view/description.phtml</template>
</action>
<action method="addTab" translate="title" module="catalog">
<alias>videos</alias>
<title>Videos</title>
<block>catalog/product_view_attributes</block>
<template>catalog/product/view/videos.phtml</template>
</action>
</block>
</reference>
<reference name="product.description">
<block type="core/template"
name="namespace.inline"
as="namespace_inline"
template="catalog/view/gallery/inline.phtml" />
</reference>
</catalog_product_view>
In my catalog/product/view/description.phtml, I have the following:
<?php echo $this->getChildHtml('namespace_inline') ?>
But the child template does not render.
I tried switching to the default theme, and it works, but the default theme does not use tabs.
Is there something going on with the Mage_Catalog_Block_Product_View_Tabs block that does not allow it to have child blocks?
Solution
You are adding your block in a reference block. Since parent block of your custom block is a reference block, it is required that, this block should be defined before it get referenced in another place.
You said, when you try it in the base theme, it works. But in your custom theme its not working. So my strong believe is that, base theme holds block defines product.description
inside it's catalog.xml
layout file. But your custom theme is not using this name for that block, instead it may be using another name.
You need to find the block's name from catalog.xml
file(in your custom theme) by referencing with it's type. (In base theme and custome theme, type
of block would be same). Then use that name in your local.xml
file.
OTHER TIPS
To find out the name of the block added as a tab, let's take a look at Mage_Catalog_Block_Product_View_Tabs::addTab()
:
function addTab($alias, $title, $block, $template)
{
if (!$title || !$block || !$template) {
return false;
}
$this->_tabs[] = array(
'alias' => $alias,
'title' => $title
);
$this->setChild($alias,
$this->getLayout()->createBlock($block, $alias)
->setTemplate($template)
);
}
The second parameter of createBlock()
is the name. So the name is the same as the alias, in your example description
.
Using the reference in layout will still not work because when the layout tree is built, the actions are not yet executed and the description
block does not exist. This means, you can not use the alias in XML as one might expect:
<reference name="description">
<block type="core/template"
name="namespace.inline"
as="namespace_inline"
template="catalog/view/gallery/inline.phtml" />
</reference>
What should work instead is creating the complete description block without reference and replace the tab child using another action
, so that it happens after the addTab
action has been executed:
<block type="catalog/product_view_description"
name="product.description"
template="catalog/product/view/description.phtml">
<block type="core/template"
name="namespace.inline"
as="namespace_inline"
template="catalog/view/gallery/inline.phtml" />
</block>
<reference name="product.info.tabs">
<action method="setChild">
<alias>description</alias>
<block>product.description</block>
</action>
</reference>
To not initialize the description block twice, you can additionally replace the original tab with a dummy placeholder, only alias and title matter:
<action method="addTab" translate="title" module="catalog">
<alias>description</alias>
<title>Description</title>
<block>core/text</block>
</action>