Magento 1.7+:如何使用页面布局手柄 [英] Magento 1.7+: How to use the page layout handle

查看:90
本文介绍了Magento 1.7+:如何使用页面布局手柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试调试了几个小时后,我没了主意,希望澄清一下(我想我有时会误解了一个概念).

After trying to debug for hours I'm out of ideas and hope for some clarification (I guess I missunderstood a concept at some point).

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

The backstory: Some base categories need an "overview page" which should be generated automatically from child categories and products. So my approach was to add a sub category to every base category and create a custom page layout which is being used from all these sub categories. For my client this would be very easy to manage in the Magento backend since he would only need to change the value in one drop down. So I created a simple module defining the new page layout. Within the backend I was able to select this one as well.

模块配置:

<?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)中引用该布局...在这里,我的逻辑离开了我:

Since these special overview pages require some layout changes I was hoping to reference the layout in a specific layout file (company_layouts.xml)... and here my logic is leaving me:

使用<layout_handle>company_category_overview</layout_handle>我希望定义一个句柄,仅当使用此特定页面模板时,才可以使用该句柄更改布局.确实不是这样.我在company_category_overview手柄内的布局更新仅被忽略.

With <layout_handle>company_category_overview</layout_handle> I was hoping to define a handle which I can use to change the layout only when this specific page template is being used. Exactly this is not the case. My layout updates which are inside the handle company_category_overview are just being ignored.

深入研究后,我意识到,这似乎不是我的代码,而更像是一个普遍的问题.在较旧的Magento 1.4安装中,页面布局句柄被携带到所有站点,例如page_one_column.在Magento 1.7和(我现在正在使用的)1.8中,只有这种情况才出现在主页上.我正在使用Commerce Bug进行调试.我只是尝试使用全新的1.7和freh 1.8的安装程序来进行此操作.

After digging deeper I realized, it doesn't seem to be my code but more like a general issue. In an old Magento 1.4 installation the page layout handle is being carried to all sites, like page_one_column. In Magento 1.7 and (what I'm using now) 1.8 this is only on the home page the case. I'm using Commerce Bug for debugging. I just tried this with a fresh 1.7 and a freh 1.8 installation.

这是我不理解的一个概念,还是一个普通的错误?

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

此外,我知道可以在后端实现布局更新,但这只是我的最后选择,因为我认为将其存放在单独的文件中更加干净,而无需复制/粘贴此类内容.

Also, I'm aware that layout updates can be achieved within the backend but this would only be my last option since I feel it's much cleaner having this in a seperate file without the need of copy/pasting such stuff.

推荐答案

这是我不理解的一个概念,还是一个普通的错误?

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

都是吗?两者都不?类别页面和CMS页面都使用<page><layout>...</layout></page>节点中的信息,但是每个系统使用该信息的方式都不同,而且两个系统都没有以您期望的方式使用它.以下是类别页面如何使用此信息的摘要.

Both? Neither? The information in the <page><layout>...</layout></page> node is used by both the category pages and CMS pages, but each system uses the the information differently, and neither system uses it in a way you'd expect. Here's a rundown on how category pages use this information.

类别页面是通过以下控制器操作呈现的

The category page is rendered by the following controller action

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

此控制器操作没有标准的loadLayoutrenderLayout方法调用.取而代之的是,此方法中有很多额外的代码,可用于添加布局句柄以及在生成块与渲染最终布局之间进行操作.我们感兴趣的部分是这个

This controller action doesn't have the standard loadLayout and renderLayout method calls. Instead, there's a lot of extra code in this method for adding layout handles and doing things between generating the blocks and rendering the final layout. The section we're interested in is this

$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方法.这是完整的方法.

When you save a category with your "Page Layout" in the Custom Design tab, the getPageLayout method call above should return company_category_overview. On category pages, Magento doesn't use this to apply a handle, instead it passes the values to the applyTemplate method. Here's that method in full.

#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);

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

which will load the information from your configuration

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

作为Varien_Object.然后,它将使用此信息将模板应用于根块.

as a Varien_Object. Then, it will use this information to apply a template to the root block.

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

因此,对于类别页面,从不使用<layout_handle/>节点中的信息.这就是为什么不应用布局更新的原因-Magento实际上会应用您的手柄.

So, for category pages, the information in the <layout_handle/> node is never used. That's why your layout updates aren't being applied — Magento actually applies your handle.

这篇关于Magento 1.7+:如何使用页面布局手柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆