Xml 类别页面的自定义布局-处理问题

Xml 类别页面的自定义布局-处理问题,xml,magento,layout,module,block,Xml,Magento,Layout,Module,Block,我有一个自定义模块,我在其中创建了新的布局 <config> <global> <page> <layouts> <category_special translate="label"> <label>Limited category</label> <template>page/limit

我有一个自定义模块,我在其中创建了新的布局

<config>
<global>
    <page>
        <layouts>
            <category_special translate="label">
                <label>Limited category</label>
                <template>page/limited_category.phtml</template>
                <layout_handle>special_limited</layout_handle>
            </category_special>
        </layouts>
    </page>
</global>

有限类别
page/limited_category.phtml
特优有限公司

有限类别出现在下拉列表中,我选择了它。 我在local.xml中编写了

<special_limited translate="label">
    <label>Category - Limited</label>
    <reference name="content">
        <block type="catalog/category_special" name="category.products" template="catalog/category/special.phtml">

        </block>
    </reference>
</special_limited>

类别有限公司
布局是使用有限的_category.phtml模板进行工作和页面加载的,但句柄不是。看起来目录\类别\默认值在此页面上

如上面链接中所述-类别控制器不调用Mage_Page_Helper_Layout::applyHandle(),因此它的自定义句柄不适用。要修复此问题,可以覆盖Mage_Catalog_CategoryController::viewAction():

public function viewAction(){
   ....
   $this->addActionLayoutHandles();
   $update->addHandle($category->getLayoutUpdateHandle());
   $update->addHandle('CATEGORY_' . $category->getId());
   if ($settings->getPageLayout()) {
            $this->getLayout()->helper('page/layout')->applyHandle($settings->getPageLayout());
        }
   ...
}

这是大多数Magento开发人员拥有的常见mus解释的完美示例。所以在这里,我试图探索更多的东西

实际上,CMS页面呈现(例如:主页)和类别页面是唯一呈现其页面的页面。表示这两个页面都手动生成布局更新句柄,然后加载并呈现它们。Magento的默认行为与这些页面完全不同。这是Magento处理正常控制器操作的方式

public function someAction()
{
    $this->loadLayout();
    $this->renderLayout();
}
$this->loadLayout()
是生成布局更新句柄然后加载它们的工具。对于CMS页面和分类页面,您可以看到这样的调用并不存在。相反,他们将通过控制器手动准备布局。我想我的观点现在更清楚了

现在让我们探讨一下
loadLayout()
在这里做了什么

public function loadLayout($handles = null, $generateBlocks = true, $generateXml = true)
{
    // if handles were specified in arguments load them first
    if (false!==$handles && ''!==$handles) {
        $this->getLayout()->getUpdate()->addHandle($handles ? $handles : 'default');
    }

    // add default layout handles for this action
    $this->addActionLayoutHandles();

    $this->loadLayoutUpdates();

    if (!$generateXml) {
        return $this;
    }
    $this->generateLayoutXml();

    if (!$generateBlocks) {
        return $this;
    }
    $this->generateLayoutBlocks();
    $this->_isLayoutLoaded = true;

    return $this;
}
在该方法中,前两行用于生成布局更新句柄。因此,让我们集中精力在这些方面。这是第一行代码

if (false!==$handles && ''!==$handles) {
    $this->getLayout()->getUpdate()->addHandle($handles ? $handles : 'default');
}
在正常情况下,这将添加
default
布局更新句柄。现在让我们看看
$this->addActionLayoutHandles()
的作用:

public function addActionLayoutHandles()
{
    $update = $this->getLayout()->getUpdate();

    // load store handle
    $update->addHandle('STORE_'.Mage::app()->getStore()->getCode());

    // load theme handle
    $package = Mage::getSingleton('core/design_package');
    $update->addHandle(
        'THEME_'.$package->getArea().'_'.$package->getPackageName().'_'.$package->getTheme('layout')
    );

    // load action handle
    $update->addHandle(strtolower($this->getFullActionName()));

    return $this;
} 
它实际上添加了3种类型的布局更新句柄。是的

  • 存储特定的布局更新句柄。例如:
    STORE\u默认值

  • 特定于主题和区域的布局更新句柄。例如:主题\前端\ rwd\默认值

  • 操作布局更新句柄ex:
    目录\类别\视图
    用于类别页面

  • 因此,如果我们总结一下,在正常情况下,Magento基本上会生成4个布局更新句柄1,而它不会生成特定于布局句柄的布局更新句柄。

    那么,布局句柄的应用是什么呢? 看起来,它们通常被定义为通过布局更新XML文件更新页面布局的捷径。ie来执行这样的操作

    <layout>
        <{some_handle}>
            <update handle="page_one_column" />
        </{some_handle}>
    </layout>
    
    因此,您不需要任何重写。可以通过单个布局udpate xml文件包含块


    1:默认情况下,Magento将生成其他布局更新句柄。它使用一个特殊事件
    controller\u action\u layout\u load\u before
    来执行此操作

    <layout>
        <catalog_category_view>
            <reference name="content">
                <block type="catalog/category_special" name="category.products" template="catalog/category/special.phtml" />
            </reference>
        </catalog_category_view>
    </layout>