Jon Koops
Jon Koops

Reputation: 9261

Overrule Magento template

I would like all my CMS pages (but not all pages) to use a custom template file, however when I use the setTemplate action in my local.xml file it's not changing the template. The block is rendering correctly but without the correct layout.

The XML I'm using right now is:

<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">

    <cms_page_view>
        <reference name="root"> 
            <action method="setTemplate"><template>page/cms-page.phtml</template></action>
        </reference>
        <reference name="right">
            <block type="catalog/navigation" name="default_page_view" template="navigation/game-menu.phtml"/>
        </reference>
    </cms_page_view>

</layout>

What am I doing wrong?

Upvotes: 1

Views: 365

Answers (1)

benmarks
benmarks

Reputation: 23205

You aren't doing anything wrong - your directive is being overridden by the entity data. For the reason why, see Mage_Cms_Helper_Page::_renderPage():

protected function _renderPage(/*...*/)
{
    //snip...
    $action->getLayout()->getUpdate()
        ->addHandle('default')
        ->addHandle('cms_page');

    $action->addActionLayoutHandles();
    if ($page->getRootTemplate()) {
        $handle = ($page->getCustomRootTemplate()
                    && $page->getCustomRootTemplate() != 'empty'
                    && $inRange) ? $page->getCustomRootTemplate() : $page->getRootTemplate();
        $action->getLayout()->helper('page/layout')->applyHandle($handle);
    }
    //snip...
}

So, your directive is being processed under the full action name handle cms_page_view, which is added via the $action->addActionLayoutHandles(); call. Whereas CMS pages are practically always saved via the admin with a root_template value, this value will always override file-based directives.

While it would be possible to update the data, it would be at risk of being overwritten when In order to provide an alternate template which will be preserved when the page is edited via the admin, it's necessary to specify some configuration values and some corresponding layout XML. In your custom module's config XML (or in app/etc/local.xml if this is a non-distributed change):

<global>
    <page>
        <layouts>
            <cms_page_custom>
                <label>Empty</label>
                <template>page/cms-page.phtml</template>
                <layout_handle>cms_page_custom</layout_handle>
            </cms_page_custom>
        </layouts>
    </page>
</global>

This will provide the option to the select input during CMS page administration. To complete this work, in your custom layout XML:

<cms_page_custom>
    <reference name="root">
        <action method="setTemplate"><template>page/cms-page.phtml</template></action>
        <!-- Mark root page block that template is applied -->
        <action method="setIsHandle"><applied>1</applied></action>
        <action method="setLayoutCode"><name>empty</name></action>
    </reference>
</cms_page_custom>

Upvotes: 3

Related Questions