2013-11-27 19 views
2

試圖調試幾個小時後,我出於想法和希望的一些澄清(我想我誤解了某個概念)。Magento 1.7+:如何使用頁面佈局句柄

背景故事:一些基本類別需要一個「概覽頁面」,它應該從子類別和產品自動生成。所以我的做法是爲每個基本類別添加一個子類別,並創建一個自定義的頁面佈局,這些佈局將從所有這些子類別中使用。對於我的客戶來說,這將很容易在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)來引用佈局一些佈局的變化......在這裏我的邏輯是離開我:

With <layout_handle>company_category_overview</layout_handle>我希望能夠定義一個句柄,只有在使用這個特定的頁面模板時,才能使用它來改變佈局。實際情況並非如此。我的佈局更新在手柄company_category_overview內被忽略。

深入挖掘後,我意識到,它似乎不是我的代碼,但更像是一個普遍的問題。在舊的Magento 1.4安裝中,頁面佈局句柄正在傳送到所有站點,如page_one_column。在Magento 1.7和(我現在使用的)1.8這只是在主頁上的情況。我正在使用Commerce Bug進行調試。我剛剛嘗試使用新的1.7和freh 1.8安裝。

這是一些概念,我不明白或只是一個普通的錯誤?

另外,我知道佈局更新可以在後端實現,但這隻會是我的最後一個選擇,因爲我覺得它更清潔,在不需要複製/粘貼這樣的東西的情況下,在單獨的文件中。

回答

5

這是一些概念,我不明白或只是一個普通的錯誤?

Both?都不是? <page><layout>...</layout></page>節點中的信息由類別頁面和CMS頁面使用,但每個系統都使用不同的信息,並且兩個系統都不以您期望的方式使用它。以下是類別頁面如何使用此信息的簡要說明。

類別頁由以下控制器動作

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

該控制器操作不會有標準loadLayoutrenderLayout方法調用呈現。相反,在這種方法中有很多額外的代碼用於添加布局句柄並在生成塊和渲染最終佈局之間進行處理。我們感興趣的部分是這個

$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加載信息。然後,它會使用這個信息將模板應用到根塊

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

因此,對於類別頁面,從不使用<layout_handle/>節點中的信息。這就是爲什麼你的佈局更新沒有被應用 - Magento實際上應用了你的手柄。

+0

感謝您的詳細澄清。深入研究1.4 Magento安裝的類別控制器我確實發現了一個'applyHandle'函數調用,它在當前版本的Magento中缺少。到目前爲止,如果在類別和產品頁面上被忽略,'layout_handle'似乎很沒用。 – themroc

+0

我需要這個 - 類別頁面處理到佈局中。我更新了我的GlobalHandle擴展來解決這個問題。它現在將選中的句柄注入。 https://github.com/ProxiBlue/GlobalHandle – proxiblue