Question: 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?

Answer: 
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:

```
 <frontend>
        <events>
            <controller_action_layout_load_before>
                <observers>
                    <add_Customer_handler>
                        <class>MymoduleModelPrefix/observer</class>
                        <method>addHandler</method>
                    </add_Customer_handler>
                </observers>
            </controller_action_layout_load_before>
        </events>
  <frontend>

```

**and Observer code is**

```
<?php
class MymoduleNameSpace_Mymodule_Model_Observer{
 public function addHandler($observer)
    {
        $loggedIn = Mage::getSingleton('customer/session')->isLoggedIn();
    if($loggedIn):
    $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
    //Get customer Group name
      $group = Mage::getModel('customer/group')->load($groupId);
      //echo $group->getCode();
        $observer->getEvent()->getLayout()->getUpdate()
             ->addHandle('customer_group_'.strtolower($group->getCode()));
    endif;
    }

}

```

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`

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

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

```
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`.

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

```

for *logged-in-customers* and

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

```

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*