2013-01-17 23 views
1

我有一些類別需要使用與我的標準類別佈局不同的佈局。無需在管理區域的「自定義設計」選項卡中重複XML代碼,最佳方式是什麼?我需要做的每個類別都是「品牌」,所以我想這可以作爲magento識別需要使用替代模板的常用方法嗎?Magento - 爲某些類別設置替代佈局

在這一點上,任何幫助表示讚賞。

謝謝

回答

4

您可以定義一個自定義的佈局句柄,並在特定的類別中調用它。

首先,定義佈局處理(例如,在你的主題local.xml中):

<layout> 
    <my_awesome_update> 
     <block ..../> 
    </my_awesome_update> 
</layout> 

然後,在後端的分類編輯頁面剛剛進入「自定義佈局更新」:

<update handle="my_awesome_update" /> 
+0

這是我的第一個想法,但更新似乎沒有生效。 – user1417156

1

如果您知道類別的ID,你可以定義在像這樣的local.xml中佈局文件中的所有的變更,也:

​​3210
0

另一種方法是使用「頁面佈局」菜單,但它需要一些額外的工作。

Page Layout Example

首先,創建一個新的擴展添加布局和觀察者(有許多教程創建一個擴展;在這裏消除了簡潔)。在新擴展的​​3210文件(我使用Bats_Coreextend供參考):

<?xml version="1.0" encoding="UTF-8"?> 
    <config> 
     <modules> 
      <Bats_Coreextend> 
       <version>0.1.0</version> 
      </Bats_Coreextend> 
     </modules> 
     <global> 
      <events> 
       <controller_action_layout_load_before> 
        <observers> 
         <addCategoryLayoutHandle> 
          <class>Bats_Coreextend_Model_Observer</class> 
          <method>addCategoryLayoutHandle</method> 
         </addCategoryLayoutHandle> 
        </observers> 
       </controller_action_layout_load_before> 
      </events> 
      <page> 
       <layouts> 
        <alt_category module="page" translate="label"> 
         <label>Alt. Category (Desc. at bottom)</label> 
         <template>page/alt-category.phtml</template> 
         <layout_handle>page_alt_category</layout_handle> 
        </alt_category> 
       </layouts> 
      </page> 
     </global> 
    </config> 

這將創建一個新的佈局,讓你引用它在菜單如上圖所示。我們需要觀察者,因爲不幸的是,即使設置頁面佈局(如上所示),您將無法引用XML中的句柄(例如catalog.xml)

要解決此問題,請創建觀察者函數:

創建app/code/local/Bats/Coreextend/Model/Observer.php

在該文件中:

<?php 

    class Bats_Coreextend_Model_Observer extends Mage_Core_Model_Observer { 
     public function addCategoryLayoutHandle(Varien_Event_Observer $observer) 
     { 
      /** @var Mage_Catalog_Model_Category|null $category */ 
      $category = Mage::registry('current_category'); 

      if(!($category instanceof Mage_Catalog_Model_Category)) { 
       return; 
      } 

      if($category->getPageLayout()) { 



       /** @var Mage_Core_Model_Layout_Update $update */ 
       $update = $observer->getEvent()->getLayout()->getUpdate(); 

       /** NOTE: May want to add an additional 
        * conditional here as this will also cause the 
        * layout handle to appear on product pages that 
        * are within the category with the alternative 
        * layout. 
        */ 

       $update->addHandle($category->getPageLayout()); 

      } 
     } 
    } 

這將alt_category手柄加入到我們的佈局,以便它可以被引用,你可以讓你的網頁進行必要的更改。

Updated Layout list with alt_category

最後,一定要創建模板文件(即page/alt-category.phtml)如上所述。