Pab*_*les 5 php magento magento-1.9
我需要能够为不同的类别使用不同的布局,在类别和页面布局字段的自定义设计选项卡上选择。
我正在使用 Magento 1.9.1.0。
在我的 config.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>
...
Run Code Online (Sandbox Code Playgroud)
在我的layouts.xml文件中,我有:
<default>
<reference name="root">...</reference>
</default>
<module_home translate="label">...</module_home>
<module_categories translate="label">...</module_categories>
Run Code Online (Sandbox Code Playgroud)
当我从类别的管理员中选择模块类别布局时,我没有得到module_categories处理程序的更改,只有在<default>.
如果我像这样强迫它:
<catalog_category_view>
<update handle="module_categories" />
</catalog_category_view>
Run Code Online (Sandbox Code Playgroud)
我确实得到了更改,但我想要多个处理程序,在管理员中选择为布局。
也许我做错了什么?找不到如何执行此操作的示例,也许您可以指点一下?谢谢!
您对该指令的想法是正确的<update />。只需将其放入Custom Layout Update管理中的类别字段中,该类别就应该应用该布局句柄。您仍然可以使用该Page Layout字段设置页面模板。
您需要使用指令显式指定布局句柄的原因<update />是因为 Magento 的类别控制器不使用该layout_handle节点,而 Magento 的其他部分(如 Magento 的 CMS 页面控制器)确实使用它。
例如,让我们看一下Mage_Cms_PageController,它负责渲染 CMS 页面:
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');
}
}
Run Code Online (Sandbox Code Playgroud)
让我们更深入地研究一下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());
}
/* ... */
}
Run Code Online (Sandbox Code Playgroud)
我们在这里看到两个重要的逻辑。
首先,_renderPage()调用$action->getLayout()->helper('page/layout')->applyHandle($handle)。如果您更深入地研究,您会发现它Mage_Page_Helper_Layout::applyHandle()负责应用layout_handle配置 XML 定义的适当内容:
public function applyHandle($pageLayout)
{
$pageLayout = $this->_getConfig()->getPageLayout($pageLayout);
if (!$pageLayout) {
return $this;
}
$this->getLayout()
->getUpdate()
->addHandle($pageLayout->getLayoutHandle());
return $this;
}
Run Code Online (Sandbox Code Playgroud)
二、_renderPage()来电$action->getLayout()->helper('page/layout')->applyTemplate($page->getRootTemplate())。与 类似applyHandle(),applyTemplate()应用实际的页面模板。
layout_handle因此,这解释了为什么在涉及 CMS 页面时您可以依赖配置 XML 中定义的内容。现在,让我们找出为什么它对类别不可靠。
让我们看一下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');
}
}
Run Code Online (Sandbox Code Playgroud)
去掉所有默认的布局逻辑,我们只剩下两部分:
// apply custom layout update once layout is loaded
if ($layoutUpdates = $settings->getLayoutUpdates()) {
if (is_array($layoutUpdates)) {
foreach($layoutUpdates as $layoutUpdate) {
$update->addUpdate($layoutUpdate);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这将遍历类别的布局更新(如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());
}
Run Code Online (Sandbox Code Playgroud)
这将应用自定义页面模板,类似于 CMS 页面逻辑的做法,使用Mage_Page_Helper_Layout::applyTemplate().
现在,注意到缺少什么了吗?
是的,类别控制器不会调用配置 XML 中定义的Mage_Page_Helper_Layout::applyHandle()应用。layout_handle这意味着您可以使用该Page Layout字段为类别提供特定的页面模板,但您layout_update附带的模板将不会被应用!
希望这能澄清为什么您的layout_update节点没有按照您期望的方式在类别页面上使用。Magento 充满了像这样的奇怪行为和不一致:)
| 归档时间: |
|
| 查看次数: |
1667 次 |
| 最近记录: |