0

I have a controller and want to define or render custom layout for different parameters passed from user to it:

    public function myactionAction() {

            $this->loadLayout();

            $params = $this->getRequest()->getParams();

            if(array_key_exists('customaction', $params)){
                     // default layout
                    ...
            }
            elseif(array_key_exists('another_subaction', $params))
            {
                   // different layout
                    ...
            }

i tried some xml in layouts:

<mymodulerouter_mycontroller_myaction_customaction>
        <reference name="root">
        <action method="setTemplate"><template>customer/custom.phtml</template></action>
    </reference>
</mymodulerouter_mycontroller_myaction_customaction>

but no success, is it possible to to this is magento or i should register another controller?

1 Answer 1

0

Yes this is possible with Magento and you don't need a new controller.

I would consider creating an observer however that observes the controller_action_layout_load_before event.

<events>
        <controller_action_layout_load_before>
            <observers>
                <mymodule_controller_action_layout_load_before>
                    <class>mymodule/observer</class>
                    <method>loadSomeLayout</method>
                </mymodule_controller_action_layout_load_before>
            </observers>
        </controller_action_layout_load_before>
    </events>

Inside your observer method you could get the request and test the parameters before adding a handle to the layout update.

class Namespace_Mymodule_Model_Observer
{
    public function loadSomeLayout($observer) {
        $layout = $observer->getEvent()->getLayout();
        $param = Mage::app()->getRequest()->getParam('hello');
        if ($param == "world")
            $layout->getUpdate()->addHandle('hello_world_default');
    }
}

Then, in your layout xml:

<hello_world_default>
        <reference name="root">
        <action method="setTemplate"><template>customer/custom.phtml</template></action>
    </reference>
</hello_world_default>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.