gpt4 book ai didi

Magento 1.7+ : How to use the page layout handle

转载 作者:行者123 更新时间:2023-12-02 21:42:03 24 4
gpt4 key购买 nike

在尝试调试几个小时后,我已经没有想法了,希望得到一些澄清(我想我在某些时候误解了一个概念)。

背景故事:某些基本类别需要一个“概述页面”,该页面应从子类别和产品自动生成。因此,我的方法是向每个基本类别添加一个子类别,并创建一个从所有这些子类别中使用的自定义页面布局。对于我的客户来说,这在 Magento 后端管理起来非常容易,因为他只需要在一个下拉列表中更改值。因此,我创建了一个定义新页面布局的简单模块。在后端我也可以选择这个。

模块配置:

<?xml version="1.0"?>
<config>
<modules>
<Company_Layouts>
<version>0.1.0</version>
</Company_Layouts>
</modules>
<global>
<page>
<layouts>
<company_category_overview module="page" translate="label">
<label>Kategorie-Übersicht</label>
<template>page/1column.phtml</template>
<layout_handle>company_category_overview</layout_handle>
</company_category_overview>
</layouts>
</page>
</global>
<frontend>
<layout>
<updates>
<company_layouts>
<file>company_layouts.xml</file>
</company_layouts>
</updates>
</layout>
</frontend>
</config>

由于这些特殊的概述页面需要一些布局更改,我希望引用特定布局文件中的布局( company_layouts.xml )...而这里我的逻辑已经离开了我:

<layout_handle>company_category_overview</layout_handle>我希望定义一个句柄,仅在使用此特定页面模板时才可以使用它来更改布局。事实并非如此。我的布局更新位于 handle 内 company_category_overview只是被忽略了。

深入挖掘后我意识到,这似乎不是我的代码,而更像是一个普遍问题。在旧的 Magento 1.4 安装中,页面布局句柄被传送到所有站点,例如 page_one_column 。在 Magento 1.7 和(我现在使用的)1.8 中,这只是在主页上的情况。我正在使用 Commerce Bug 进行调试。我刚刚尝试了新的 1.7 和新的 1.8 安装。

这是我不理解的概念还是只是一个简单的错误?

此外,我知道布局更新可以在后端实现,但这只是我的最后一个选择,因为我觉得将其放在单独的文件中会更干净,而不需要复制/粘贴此类内容。

最佳答案

Is this some concept I don't understand or just a plain bug?

两者都?两者都不?信息在 <page><layout>...</layout></page>类别页面和 CMS 页面都使用节点,但每个系统使用信息的方式不同,并且两个系统都不会以您期望的方式使用它。以下是类别页面如何使用此信息的概要。

类别页面由以下 Controller 操作呈现

#File: app/code/core/Mage/Catalog/controllers/CategoryController.php
public function viewAction()
{
...
}

此 Controller 操作没有标准 loadLayoutrenderLayout方法调用。相反,此方法中有很多额外的代码,用于添加布局句柄以及在生成 block 和渲染最终布局之间执行操作。我们感兴趣的部分是这个

$design = Mage::getSingleton('catalog/design');
$settings = $design->getDesignSettings($category);

#...other stuff we don't care about...

if ($settings->getPageLayout()) {
$this->getLayout()->helper('page/layout')->applyTemplate($settings->getPageLayout());
}

当您在“自定义设计”选项卡中保存带有“页面布局”的类别时,getPageLayout上面的方法调用应该返回 company_category_overview 。在类别页面上,Magento 不使用它来应用句柄,而是将值传递给 applyTemplate方法。这是该方法的完整内容。

#File: app/code/core/Mage/Page/Helper/Layout.php
public function applyTemplate($pageLayout = null)
{
if ($pageLayout === null) {
$pageLayout = $this->getCurrentPageLayout();
} else {
$pageLayout = $this->_getConfig()->getPageLayout($pageLayout);
}

if (!$pageLayout) {
return $this;
}

if ($this->getLayout()->getBlock('root') &&
!$this->getLayout()->getBlock('root')->getIsHandle()) {
// If not applied handle
$this->getLayout()
->getBlock('root')
->setTemplate($pageLayout->getTemplate());
}

return $this;
}

相关部分是这一行,

$pageLayout = $this->_getConfig()->getPageLayout($pageLayout);

这将从您的配置中加载信息

<label>Kategorie-Übersicht</label>
<template>page/1column.phtml</template>
<layout_handle>company_category_overview</layout_handle>

作为Varien_Object 。然后,它将使用此信息将模板应用到根 block

$this->getLayout()
->getBlock('root')
>setTemplate($pageLayout->getTemplate());

因此,对于类别页面,<layout_handle/> 中的信息节点从未被使用过。这就是您的布局更新未被应用的原因 — Magento 实际上应用了您的句柄。

关于Magento 1.7+ : How to use the page layout handle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20249894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com