I need to change default layout file (1column.phtml, 2columns-left.phtml, etc.) based on currently logged in user (user group) and if he's logged in or not.

Is changing the default template possible from inside the PHP code, I mean just layout, I know how to do PHP logic?

4 Answers 4


Another way to this create different handler for each customer group using event controller_action_layout_load_before then on layout xml set it template.

Config.xml code is:


and Observer code is

class MymoduleNameSpace_Mymodule_Model_Observer{
 public function addHandler($observer)
        $loggedIn = Mage::getSingleton('customer/session')->isLoggedIn();
    $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
    //Get customer Group name
      $group = Mage::getModel('customer/group')->load($groupId);
      //echo $group->getCode();


According to observer code , it create handler like

General Group Customer :customer_group_general whole Group Customer : customer_group_wholesales etc.

Logic like: customer_group_LOweCaseOfCustomergroupName

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

    <reference name="root">
        <action method="setTemplate"><template>page/2columns-left.phtml</template></action>
  • yup only if my method does not satisfy your conditions. In such case, if you want to control more customer groups, then this would be the best solution. +1 Amit Commented Apr 28, 2015 at 6:54

You can change the template (layout file) in your controller.

    public function indexAction() {
        if (yourcondition){ // e.g. check user group
            $this->getLayout()->getBlock('root')->setTemplate('page/templatefile.phtml');  //changes the template
        return $this;
  • Thanks Alvin. This solution is to the point and technically correct. It won't work in my case as I'm using Mage_Catalog_CategoryController override but I haven't mentioned it in my question :).
    – Alan
    Commented Apr 29, 2015 at 12:51

The best and recommended way is using the layout handle that is uniquely created for this purpose. They are non another than customer_logged_in and customer_logged_out.

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

for logged-in-customers and

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

for not-logged-in customers.

Note 1: These two layout handles will be processed very early. So there is a chance to rewrite the root template by other layout handles which will processed after these layout handles.

Note 2 : Change the template file that you needed in the above example code as per your need.

Note 2 : If you want to change the template based on the customer groups, then you should go with programmatic ways

  • Thanks Rajeev. That's a great answer but I need to change layout programmatically, depending on the combination of log in status and customer group.
    – Alan
    Commented Apr 29, 2015 at 12:57
  • you can try amith's answer in that case Commented Apr 29, 2015 at 13:03

Untested, but this should work:

$layout = Mage::app()->getLayout();
$roleId = Mage::getSingleton('customer/session')->getCustomerGroupId();
$role = Mage::getSingleton('customer/group')->load($roleId)->getData('customer_group_code');

if($role == "Wholesale") {
    $layout->getUpdate()->addUpdate('<reference name="root"><action method="setTemplate"><template>page/1column.phtml</template></action></reference>');


  • 1
    Thanks for the answer and reference. It didn't work in my case as I was calling it from a controller override but may work from an observer(as the reference link indicates).
    – Alan
    Commented Apr 29, 2015 at 12:47

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.