【发布时间】:2013-01-01 07:01:25
【问题描述】:
我有许多类别需要使用与标准类别布局不同的布局。无需在管理区域的“自定义设计”选项卡中重复 XML 代码的最佳方法是什么?我需要为其执行此操作的每个类别都是一个“品牌”,所以我想这可以用作 magento 识别需要使用替代模板的常用方法?
感谢您的帮助。
谢谢
【问题讨论】:
标签: xml magento templates layout categories
我有许多类别需要使用与标准类别布局不同的布局。无需在管理区域的“自定义设计”选项卡中重复 XML 代码的最佳方法是什么?我需要为其执行此操作的每个类别都是一个“品牌”,所以我想这可以用作 magento 识别需要使用替代模板的常用方法?
感谢您的帮助。
谢谢
【问题讨论】:
标签: xml magento templates layout categories
您可以定义自定义布局句柄并在特定类别中调用它。
首先,定义您的布局句柄(例如在主题的 local.xml 中):
<layout>
<my_awesome_update>
<block ..../>
</my_awesome_update>
</layout>
然后,在后台的分类编辑页面中输入“自定义布局更新”:
<update handle="my_awesome_update" />
【讨论】:
如果您知道类别的 ID,您也可以像这样在 local.xml 布局文件中定义所有更改:
<layout>
<CATEGORY_4>
<!-- updates here -->
</CATEGORY_4>
</layout>
【讨论】:
另一种选择是使用“页面布局”菜单,尽管它需要一些额外的工作。
首先,创建一个新扩展以添加布局和观察者(有许多创建扩展的教程;为简洁起见,此处省略)。在你的新扩展的config.xml 文件中(我使用Bats_Coreextend 作为参考):
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Bats_Coreextend>
<version>0.1.0</version>
</Bats_Coreextend>
</modules>
<global>
<events>
<controller_action_layout_load_before>
<observers>
<addCategoryLayoutHandle>
<class>Bats_Coreextend_Model_Observer</class>
<method>addCategoryLayoutHandle</method>
</addCategoryLayoutHandle>
</observers>
</controller_action_layout_load_before>
</events>
<page>
<layouts>
<alt_category module="page" translate="label">
<label>Alt. Category (Desc. at bottom)</label>
<template>page/alt-category.phtml</template>
<layout_handle>page_alt_category</layout_handle>
</alt_category>
</layouts>
</page>
</global>
</config>
这将创建新布局并允许您在菜单中引用它,如上图所示。我们需要观察者,因为不幸的是,即使设置了页面布局(如上所示),您也无法在 XML 中引用句柄(例如 catalog.xml)
为了解决这个问题,创建观察者函数:
创建app/code/local/Bats/Coreextend/Model/Observer.php
在那个文件中:
<?php
class Bats_Coreextend_Model_Observer extends Mage_Core_Model_Observer {
public function addCategoryLayoutHandle(Varien_Event_Observer $observer)
{
/** @var Mage_Catalog_Model_Category|null $category */
$category = Mage::registry('current_category');
if(!($category instanceof Mage_Catalog_Model_Category)) {
return;
}
if($category->getPageLayout()) {
/** @var Mage_Core_Model_Layout_Update $update */
$update = $observer->getEvent()->getLayout()->getUpdate();
/** NOTE: May want to add an additional
* conditional here as this will also cause the
* layout handle to appear on product pages that
* are within the category with the alternative
* layout.
*/
$update->addHandle($category->getPageLayout());
}
}
}
这会将alt_category 句柄添加到我们的布局中,以便可以引用它,并且您可以对您的页面进行必要的更改。
最后,请务必按照上述说明创建模板文件(即page/alt-category.phtml)。
【讨论】: