22

I have a custom controller accessed at the path /custommodule/customer/info which is loading the 2columns-left.phtml template on the root node as follows:

<custommodule_customer_info>
    <reference name="root">
        <action method="setTemplate"><template>page/2columns-left.phtml</template></action>
    </reference>
</custommodule_customer_info>

What I would like to do is update the root template for customers who are not logged in, something such as:

<customer_logged_out>
    <custommodule_customer_info>
        <reference name="root">
            <action method="setTemplate"><template>page/1column.phtml</template></action>
        </reference>
    </custommodule_customer_info>
</customer_logged_out>

I understand that I can't target multiple layout handles in this fashion, but the intent should be clear; update the root template for this layout handle while customers are not logged in.

I had thought that I could target my controller's handle with the following:

<customer_logged_out>
    <reference name="custommodule_customer_info">
        <reference name="root">
            <action method="setTemplate"><template>page/1column.phtml</template></action>
        </reference>
    </reference>
</customer_logged_out>

This indeed does update the root template with the 1column.phtml template, but it is doing so on what appears to be all pages instead of just the page targeted in my reference node.

I have tried several permutations of this layout update, but none seem to work. How can I target this one layout handle while at the same time using the customer_logged_out layout handle?

-- edit -- To be clear, this is actually a third-party module.

18

Since you are using your own controller you don't need to only use the default handles. Based on the logged in status you could add in your infoAction method

 $this->getLayout()->getUpdate()->addHandle('mymodule_customer_info_logged_in');

or

 $this->getLayout()->getUpdate()->addHandle('mymodule_customer_info_logged_out');

and then in your layout.xml file use

<mymodule_customer_info_logged_in>

and

<mymodule_customer_info_logged_out>

-- addition after your edit below --

Seeing that you don't want to edit the controller (since it is a 3rd party extension), I would create a separate extension that only observes controller_action_layout_load_before

    $update = $observer->getEvent()->getLayout()->getUpdate();
    $handles = $update->getHandles();

    if (in_array('custommodule_customer_info', $handles)) {

        //code to add the custom handles based on login
    }
  • Snazzy. This is not ideal, but is a relatively clean solution. As I mentioned in my other comment below, I'd rather not have to modify a third-party module, but it looks like there isn't much of a choice here. Shame, because the customer_logged_in/out handles could be so powerful, but they get gimped because of this limitation. – pspahn Jan 24 '13 at 17:25
14

Alan Storm has answered something similair like this:

https://stackoverflow.com/a/5601579/1157493

You can use a helper to do your logic of checking whether the custom in logged in.

<action method="setTemplate">
    <template helper="mymodule/myhelper/switchTemplateIf"/>
</action>

It will call Mage::helper('mymodule/myhelper')->switchTemplateIf(); In that helper you can decide to switch the template or just keep it like this.

I believe it sets the template to whatever you return in that function.

It would look something like this:

public function switchTemplateIf() {
    if (Mage::helper('customer')->isLoggedIn()) {
        return 'page/1column.phtml';
    } else {
        return 'page/2column-right.phtml';
    }
}

Untested

  • I did have this thought as well, but I was hoping to only have to rely on the module's layout XML to perform this without having to rely on additional classes in the module - the philosophy of "if it can be done in XML, do it in XML, don't create additional module resources if it's not necessary". – pspahn Jan 24 '13 at 17:03
  • @pspahn Of course the goal is to try and do it with the XML layout system Magento provides, unfortunately it does have its limitations and I believe this is one of them. Although I still think this is a perfectly viable solution despite the extra module resource. – Rick Kuipers Jan 24 '13 at 17:06
  • @pspahn Fooman has a good alternative solution which I would recommend over mine. It requires less coding! – Rick Kuipers Jan 24 '13 at 17:10
  • In this case, I'm actually dealing with a third-party module. I'd rather not have to edit their module (since updates may break it) and I'd rather not place this in a separate module's helper class (since that ties their module and module together at the hip). – pspahn Jan 24 '13 at 17:10
6

I recently needed this kind of functionality & it was getting difficult adding more & more layout handles for all the different combinations of existing layout handles, so i created a magento extension to add the ability of targeting multiple layout handles from the layout xml directly.

Here's the link to the extension- https://github.com/mridul89/MultipleHandles.git

You would use it like this-

<customer_logged_out ifhandle="custommodule_customer_info">
    <reference name="root">
        <action method="setTemplate"><template>page/1column.phtml</template></action>
    </reference>
</customer_logged_out>

This will tell magento to only use this particular customer_logged_out layout handle if custommodule_customer_info layout handle is also present.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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