php – Magento上catalog_category_view的自定义布局
我需要能够在类别和页面布局字段的“自定义设计”选项卡上选择不同的类别使用不同的布局.
我正在使用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> ... 在我的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 />的正确想法.指示.只需将它放在管理员的类别的自定义布局更新字段中,该类别应该应用该布局句柄.您仍然可以使用“页面布局”字段设置页面模板.
您需要使用< 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'); } } 让我们深入挖掘并查看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); } } } 这将通过类别的布局更新(在管理员的“自定义布局更新”字段中定义)并应用它们.这就是使用< 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()); } 这将使用Mage_Page_Helper_Layout :: applyTemplate()应用自定义页面模板,类似于CMS页面逻辑的工作方式. 现在,注意到缺少的东西? 是的,类别控制器不会调用Mage_Page_Helper_Layout :: applyHandle()来应用配置XML中定义的layout_handle.这意味着您可以使用“页面布局”字段为类别指定特定页面模板,但模板附带的layout_update将不会应用! 希望这可以清除为什么你的layout_update节点没有像你期望的那样在类别页面上使用. Magento充满了奇怪的行为和不一样的东西:) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |