我需要能够对不同的类别使用不同的布局,在类别和页面布局字段的自定义设计选项卡上选择。
我正在使用Magento 1.9.1.0。
在我的配置中.xml我有:
<global>
<page>
<layouts>
<module_home module="page" translate="label">
<label>Module Home</label>
<template>page/base.phtml</template>
<layout_handle>module_home</layout_handle>
</module_home>
<module_categories module="page" translate="label">
<label>Module Categories</label>
<template>page/base.phtml</template>
<layout_handle>module_categories</layout_handle>
</module_categories>
</layouts>
</page>
...
在我的layouts.xml
文件中,我有:
<default>
<reference name="root">...</reference>
</default>
<module_home translate="label">...</module_home>
<module_categories translate="label">...</module_categories>
当我从类别的管理员中选择模块类别布局时,我没有收到module_categories处理程序的更改,只有<default>
上设置的更改。
如果我像这样强迫它:
<catalog_category_view>
<update handle="module_categories" />
</catalog_category_view>
我确实得到了更改,但我想要多个处理程序,在管理员上选择为布局。
也许我做错了什么?找不到如何做到这一点的示例,也许您可以指出某个地方?谢谢!
你对<update />
指令有正确的想法。只需将其放在管理员的类别Custom Layout Update
字段中,该类别应应用该布局句柄。您仍然可以使用 Page Layout
字段设置页面模板。
您需要使用 <update />
指令显式指定布局句柄的原因是 Magento 的类别控制器不使用layout_handle
节点,而 Magento 的其他部分(如 Magento 的 CMS 页面控制器)确实使用它。
例如,让我们看一下负责呈现CMS页面的Mage_Cms_PageController
:
public function viewAction()
{
$pageId = $this->getRequest()
->getParam('page_id', $this->getRequest()->getParam('id', false));
if (!Mage::helper('cms/page')->renderPage($this, $pageId)) {
$this->_forward('noRoute');
}
}
让我们更深入地挖掘并查看Mage_Cms_Helper_Page::renderPage()
,它调用Mage_Cms_Helper_Page::_renderPage()
:
protected function _renderPage(Mage_Core_Controller_Varien_Action $action, $pageId = null, $renderLayout = true)
{
$page = Mage::getSingleton('cms/page');
/* ... */
if ($page->getRootTemplate()) {
$handle = ($page->getCustomRootTemplate()
&& $page->getCustomRootTemplate() != 'empty'
&& $inRange) ? $page->getCustomRootTemplate() : $page->getRootTemplate();
$action->getLayout()->helper('page/layout')->applyHandle($handle);
}
/* ... */
if ($page->getRootTemplate()) {
$action->getLayout()->helper('page/layout')
->applyTemplate($page->getRootTemplate());
}
/* ... */
}
我们在这里看到了两个重要的逻辑。
首先,_renderPage()
调用$action->getLayout()->helper('page/layout')->applyHandle($handle)
。如果更深入地挖掘,您会发现Mage_Page_Helper_Layout::applyHandle()
负责应用配置 XML 定义的相应layout_handle
:
public function applyHandle($pageLayout)
{
$pageLayout = $this->_getConfig()->getPageLayout($pageLayout);
if (!$pageLayout) {
return $this;
}
$this->getLayout()
->getUpdate()
->addHandle($pageLayout->getLayoutHandle());
return $this;
}
其次,_renderPage()
打电话给$action->getLayout()->helper('page/layout')->applyTemplate($page->getRootTemplate())
。与applyHandle()
类似,applyTemplate()
应用实际的页面模板。
因此,这就解释了为什么在涉及CMS页面时,您可以依赖配置XML中定义的layout_handle
。现在,让我们找出为什么它对类别不可靠。
让我们看一下Mage_Catalog_CategoryController::viewAction()
,它负责显示一个类别页面:
public function viewAction()
{
if ($category = $this->_initCatagory()) {
$design = Mage::getSingleton('catalog/design');
$settings = $design->getDesignSettings($category);
/* ... */
// apply custom layout update once layout is loaded
if ($layoutUpdates = $settings->getLayoutUpdates()) {
if (is_array($layoutUpdates)) {
foreach($layoutUpdates as $layoutUpdate) {
$update->addUpdate($layoutUpdate);
}
}
}
/* ... */
// apply custom layout (page) template once the blocks are generated
if ($settings->getPageLayout()) {
$this->getLayout()->helper('page/layout')->applyTemplate($settings->getPageLayout());
}
/* ... */
}
elseif (!$this->getResponse()->isRedirect()) {
$this->_forward('noRoute');
}
}
去除所有默认布局逻辑,我们只剩下两部分:
// apply custom layout update once layout is loaded
if ($layoutUpdates = $settings->getLayoutUpdates()) {
if (is_array($layoutUpdates)) {
foreach($layoutUpdates as $layoutUpdate) {
$update->addUpdate($layoutUpdate);
}
}
}
这将遍历类别的布局更新(如管理员Custom Layout Update
字段中所定义)并应用它们。这就是为什么使用<update handle="some_handle" />
有效的原因。
和。。。
// apply custom layout (page) template once the blocks are generated
if ($settings->getPageLayout()) {
$this->getLayout()->helper('page/layout')->applyTemplate($settings->getPageLayout());
}
这将应用自定义页面模板,类似于 CMS 页面逻辑使用 Mage_Page_Helper_Layout::applyTemplate()
的方式。
现在,发现缺少一些东西了吗?
是的,类别控制器不会调用Mage_Page_Helper_Layout::applyHandle()
来应用配置 XML 中定义的layout_handle
。这意味着您可以使用Page Layout
字段为类别提供特定的页面模板,但模板随附的layout_update
不会被应用!
希望这能澄清为什么您的layout_update
节点没有按照您期望的方式在类别页面上使用。Magento充满了奇怪的行为和不一致的地方,比如:)