Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally remove header/footer in Magento

I have a module page that can be accessed like

www.example.com/module/controller/action/id/10

I want something like this in my controller's action

$page = (int) Mage::app()->getRequest()->getParam('id');
if($page == '12')
{
    $this->getLayout()->unsetBlock('header');
    $this->getLayout()->unsetBlock('footer');
}

But above method doesn't work, I guess I'm passing wrong alias to unsetBlock method.

I know how to hide header/footer via layout xml but here I want to hide them in controller.

So basically I am searching an alternative for

<remove name="header"/>
<remove name="footer"/> 
like image 931
Tahir Yasin Avatar asked Oct 20 '25 03:10

Tahir Yasin


1 Answers

I found solution to my own question, sharing because it may help others.

1. Create a new layout handle for the page

// Namespace/Modulename/Model/Observer.php
Class Namespace_Modulename_Model_Observer extends Mage_Core_Model_Abstract
{

    public function addAttributeSetHandle(Varien_Event_Observer $observer)
    {
        $page = (int) Mage::app()->getRequest()->getParam('id');
        $handle = sprintf('modulename_controller_action_id_%s', $page);
        $update = $observer->getEvent()->getLayout()->getUpdate();
        $update->addHandle($handle);
    }
}

2. Enable the observer in module's config.xml

// Namespace/Modulename/etc/config.xml
<frontend>
    <events>
        <controller_action_layout_load_before>
            <observers>
                <attributesethandle>
                    <class>Namespace_Modulename_Model_Observer</class>
                    <method>addAttributeSetHandle</method>
                </attributesethandle>
            </observers>
        </controller_action_layout_load_before>
    </events>
</frontend>

3. And then easily change the layout for the handle modulename_controller_action_id_12 in modules layout xml.

<modulename_controller_action_id_12>
    <remove name="header"/>
    <remove name="footer"/>
    <reference name="root">
        <action method="setTemplate">
            <template>page/1column.phtml</template>
        </action>
    </reference>
</modulename_controller_action_id_12>
like image 98
Tahir Yasin Avatar answered Oct 21 '25 16:10

Tahir Yasin