Magento – If condition in layout xml tag

configurationmagento-1magento-1.7xml

I have create one module for displaying latest product review, module works perfectly and reviews are display on frontend side.

But what I have requirement is admin can select where to display this block on frontend side, either on right sidebar or left sidebar or in main content area.

I have provided dropdown selection for admin to select one layout for display, but on frontend how we can determine which layout to use for display of block, can we put condition in layout xml? Or there is another method to detect?

We can give ifconfig in action tag but its not suitable for my requirement.

What I have do right now is display block in left bar, and block is display in left side bar.

<reference name="left">
<my block />
</reference>

Best Answer

What you have is a widget and you should implement it as such. Widgets have a very handy layout selection interface already made for you.

If you want to implement this without using the widget interface, you need a block method which will set the parent block, e.g.:

protected function _setParentFromSetting($parent = 'left')
{
    $parent = $this->getLayout()->getBlock($parent);
    if ($parent) {
        $parent->insert($this);
    }
}

You would then want to ensure that this method is called when the block is instantiated:

protected function _prepareLayout()
{
    // logic to read setting
    // e.g. $parent = Mage::getStoreConfig('your/config/path');
    $this->_setParentBlock($parent);

    return parent::_prepareLayout();
}