Magento:以编程方式添加子块

时间:2014-10-17 03:41:35

标签: magento

我在布局文件中有这个块:

<block type="core/template_facade" name="product.info.container2" as="container2">
                    <action method="setDataByKey"><key>alias_in_layout</key><value>container2</value></action>
                    <action method="setDataByKeyFromRegistry"><key>options_container</key><key_in_registry>product</key_in_registry></action>
                    <action method="append"><block>product.info.options.wrapper</block></action>
                    <action method="append"><block>product.info.options.wrapper.bottom</block></action>
                </block>

这块:

$item_block = Mage::getSingleton('core/layout')->createBlock('catalog/product_list','item')->setTemplate('sns/ajaxcart/catalog/product/item.phtml')->setData('product', $product);

如何以编程方式将上部块作为$ item_block的子项?我知道它应该是这样的:

$upper_block = <create upper block>;
$item_block->setChild('somealias',$upper_block);

但是,上层块似乎太复杂了,我不知道如何以编程方式创建它。请帮忙!

2 个答案:

答案 0 :(得分:2)

在块级别,您可以这样做:

protected function _prepareLayout()
{
    $block = $this->getLayout()->createBlock('catalog/product_list')
    $this->setChild('items', $block);
    return parent::_prepareLayout();
}

答案 1 :(得分:0)

试试这个

$upper_block = Mage::getSingleton('core/layout') ->createBlock('core/template_facade', "product.info.container2");
$upper_block->setDataByKey('alias_in_layout', 'container2');
$upper_block->setDataByKeyFromRegistry('options_container', 'product');
$upper_block->append('product.info.options.wrapper');
$upper_block->append('product.info.options.wrapper.bottom');

$item_block = Mage::getSingleton('core/layout')->createBlock('catalog/product_list','item')->setTemplate('sns/ajaxcart/catalog/product/item.phtml')->setData('product', $product);

$item_block->setChild('somealias',$upper_block);