Conditionally remove header / footer in Magento

I have a module page that can be accessed, for example

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

I need something like this in my controller action

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

But the method does not work above, I assume that I am passing the wrong alias to the method unsetBlock.

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

So basically I'm looking for an alternative for

<remove name="header"/>
<remove name="footer"/> 
+4
source share
1 answer

I found a solution to my question using it because it can help others.

1. Create a new layout descriptor 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. 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. modulename_controller_action_id_12 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>
+6

Source: https://habr.com/ru/post/1536460/


All Articles