为了在您的local.xml
文件中使用自定义布局句柄,首先您必须为其创建一个观察者。要创建观察者,首先将其添加为扩展/模块。如果不存在,请创建以下文件/文件夹(名称Yourname
和Modulename
可以是任何东西,只要确保它显示的位置相同,包括大写/小写):
目录视图
/app/etc/modules/Yourname_Modulename.xml
/app/code/local/Yourname/Modulename/etc/config.xml
/app/code/local/Yourname/Modulename/Model/Observer.php
现在你已经有了文件结构,让我们看看第一个文件,Yourname_Modulename.xml 隐藏在 app/etc/modules/ 文件夹中:
Yourname_Modulename.xml
<?xml version="1.0"?>
<config>
<modules>
<Yourname_Modulename>
<codePool>local</codePool>
<active>true</active>
</Yourname_Modulename>
<modules>
<config>
现在 /app/code/local/Yourname/Modulename/etc/config.xml:
配置文件
<?xml version="1.0"?>
<config>
<global>
<models>
<yournamemodulename>
<class>Yourname_Modulename_Model</class>
</yournamemodulename>
</models>
</global>
<frontend>
<events>
<controller_action_layout_load_before>
<observers>
<yourname_modulename_model_observer>
<type>singleton</type>
<class>Yourname_Modulename_Model_Observer</class>
<method>controllerActionLayoutLoadBefore</method>
</yourname_modulename_model_observer>
</observers>
</controller_action_layout_load_before>
</events>
</frontend>
</config>
最后是文件 /app/code/local/Yourname/Modulename/Model/Observer.php。对于这个,你需要知道你想给“your_layout_handle”取什么名字,以及如何确定你的布局是否应该通过 PHP 加载。
观察者.php
<?php
class Yourname_Modulename_Model_Observer
{
public function controllerActionLayoutLoadBefore( Varien_Event_Observer $observer)
{
//Get Layout Object
$layout = $observer->getEvent()->getLayout();
/*
*Begin Logic to Determine If Layout Handle Should Be Applied.
*Below Determines If We Are On A Product View Page.
*Here is Where You Would Modify The Code For Different Layout Handles
*/
if( Mage::registry( 'current_product' ) ) {
//Check if current_category is set
if( Mage::registry( 'current_category' ) ) {
//Send Layout Update Handle If Product Was Browsed
$layout->getUpdate()->addHandle( 'your_layout_handle' );
}
else {
//Send Layout Update Handle If Product Was Linked or Searched
$layout->getUpdate()->addHandle( 'your_other_handle' );
}
}
}
}
我想说这就是全部,但当然你现在需要对 app/code/design/frontend/package/theme/layout/local.xml 中的布局句柄做一些事情。它的行为方式取决于您,但这里的示例是适用于我的 local.xml 的部分。我用于句柄的名称是“catalog_product_view_browsed”和“catalog_product_view_searched”。
本地.xml
<!-- Jump To Relevant Section -->
<catalog_product_view_browsed>
<reference name="left">
<action method="unsetChild">
<name>left.poll</name>
</action>
</reference>
<reference name="right">
<action method="insert">
<blockName>left.poll</blockName>
<siblingName>right.newsletter</siblingName>
<after>0</after>
</action>
</reference>
</catalog_product_view_browsed>
<catalog_product_view_searched>
<reference name="left">
<action method="insert">
<blockName>right.newsletter</blockName>
<siblingName>left.vertnav</siblingName>
<after>1</after>
</action>
</reference>
<reference name="right">
<action method="unsetChild">
<name>right.newsletter</name>
</action>
</reference>
</catalog_product_view_browsed>
<!-- End Relevant Section -->
您可能需要刷新/清理缓存。应该是这样的。