September 5, 2012

How to set a default layout in Magento 1.5 using local.xml?

Question by Reed Richards

So I’ve done some layouts that I want to use and I was thinking that setting this in your local.xml file would fix this for every page. Like this

<default>
<reference name="root">
    <action method="setTemplate">
      <template>page/mytheme.phtml</template>
    </action>
</reference>
</default>

This however doesn’t do anything. Instead if I go

...<customer_account_forgotpassword>
  <reference name="root">
    <action method="setTemplate"><template>page/mytheme.phtml</template></action>
  </reference>
</customer_account_forgotpassword>  

<customer_account_confirmation>
  <reference name="root">
    <action method="setTemplate"><template>page/mytheme.phtml</template></action>
  </reference>
</customer_account_confirmation>...

and so on for every specific place it gets changed on those specific pages. Am I thinking wrong or this the only way to do it?

Answer by Alan Storm

The problem you’re (probably) running into is that something comes along later and sets the template for the root block again, overriding your change.

More recent versions of Magento (1.4somethingish) introduced a way to prevent this from happening. If you look in page.xml, you’ll see a handle like this

<page_one_column translate="label">
    <label>All One-Column Layout Pages</label>
    <reference name="root">
        <action method="setTemplate"><template>page/1column.phtml</template></action>
        <!-- Mark root page block that template is applied -->
        <action method="setIsHandle"><applied>1</applied></action>
    </reference>
</page_one_column>

If the page_one_column handle gets applied to your request, or you manually apply it with

<update handle="page_one_column" />

Magento will change the template and call the setIsHandle method on the block.

<action method="setIsHandle"><applied>1</applied></action>

There’s code in Magento that will look for the IsHandle property, and if it’s true, further calls to setTemplate will be ignored. (that’s over-simplifying things a bit, but is more or less true)

Try something like

<default>
    <reference name="root">
        <action method="setTemplate">
          <template>page/mytheme.phtml</template>
        </action>
        <action method="setIsHandle">
            <applied>1</applied>
        </action>       
    </reference>
</default>  

And see if that helps.

Keep in mind this is one of those grey areas where it’s not clear if third party developers are supposed to tread. Also, changing the template for all pages may have un-desirable effect on parts of the site that expect to be in a certain template.

Answer by Starx

Actually, you are the on the right track. You just didn’t specify the request on your local.xml. You should also include the request you are overriding.

Here is a example code of local.xml

<layout>
    <default>
    ....
    </default>

    <!-- Update Configuration for this request specially -->
    <customer_account_confirmation> 
      <reference name="root">
        <action method="setTemplate"><template>page/mytheme.phtml</template></action>
      </reference>
    </customer_account_confirmation>

</layout>

This much configuration is enough to do, what you need.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!