开发者

How to set custom template for home page?

I've tried to set 1column template for home page using my local.xml file:

<cms_index_index>
    <reference name="root">
        <action method="setTemplate"><template>page/1column.phtml</template></acti开发者_StackOverflowon>
    </reference>
</cms_index_index>

But this is doesn't work. How can I do this?


Homepage is a CMS page. Unfortunately, you can't assign root template for CMS pages using layout, because they have own attribute "root_template" (cms_page table). You can change this attribute in the backend (CMS - Pages). Or you can change it in code:

$homePage = Mage::getModel('cms/page')->load('home', 'identifier');
$homePage->setRootTemplate('one_column');
$homePage->save();

I recommend you to write sql data upgrade, which will update root template value for homepage:

$installer = $this;
/* @var $installer Mage_Core_Model_Resource_Setup */

$installer->startSetup();

$installer->run("
    UPDATE `{$this->getTable('cms_page')}` SET `root_template` = 'one_column' WHERE `identifier` = 'home';
");

$installer->endSetup();


I'm sure the other suggestions work well, but that all looks way too complicated to me. What I've done which seems to work great, is to simply put the following into the Layout Update XML for the CMS page in question (in this case, your home page)

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


The problem lays in Mage_Cms_Helper_Page::_renderPage. Layout updates are applied -before- root template (configured from the backend) is applied:

Mage::dispatchEvent('cms_page_render', array('page' => $page, 'controller_action' => $action));

$action->loadLayoutUpdates();
$layoutUpdate = ($page->getCustomLayoutUpdateXml() && $inRange) ? $page->getCustomLayoutUpdateXml() : $page->getLayoutUpdateXml();
$action->getLayout()->getUpdate()->addUpdate($layoutUpdate);
$action->generateLayoutXml()->generateLayoutBlocks();

...

if ($page->getRootTemplate()) {
    $action->getLayout()->helper('page/layout')
        ->applyTemplate($page->getRootTemplate());
}

Also notice how the only event in this method is inconveniently placed above all of this... Should you want to fix this cleanly (without queries), you should observe the following event:

controller_action_postdispatch_cms_index_index

Then do the following (untested, but should work):

$this->getEvent()->getControllerAction()->getLayout()->helper('page/layout')->applyTemplate('one_column');

Then render the layout again. This is all just a guideline how to solve this through observers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜