2012-07-27 32 views
2

我有一個通用的模板文件,我想用於每一頁。我親眼目睹一位同事將一個參數添加到他的local.xml或page.xml佈局文件中,該文件強制每個頁面都使用特定佈局文件,而不管數據庫值如何。他離開了公司,所以我不能問他他是怎麼做到的。我嘗試了How to set a default layout in Magento 1.5 using local.xml?中建議的答案,但那不起作用。如何'強制'Magento加載只有一個模板

如何強制Magento的(1.7)僅加載模板/我想要佈局?

回答

3

這是遺憾的,但不是說你可以用它來確保所有的頁面佈局會使用你的模板只是一個選項,但你可以申請,將設置您的自定義模板中的每個頁面佈局的佈局手柄處理。

所以在你local.xml中應該有類似以下內容:

<your_custom_handle> 
    <action method="setTemplate" block="root"><template>your/template.phtml</template></action> 
</your_custom_handle> 

<page_empty> 
    <update handle="your_custom_handle" /> 
</page_empty> 

<page_one_column> 
    <update handle="your_custom_handle" /> 
</page_one_column> 

<page_two_columns_left> 
    <update handle="your_custom_handle" /> 
</page_two_columns_left> 

<page_two_columns_right> 
    <update handle="your_custom_handle" /> 
</page_two_columns_right> 

<page_three_columns> 
    <update handle="your_custom_handle" /> 
</page_three_columns> 

佈局更新的第一部分,創建自己的手柄,即設置模板,根塊。所有其他人都將您的自定義句柄包含到不同的頁面佈局中。

享受!

+1

雖然做這我需要在網站上包含任何類型的頁面的句柄。在這些地方有沒有確定的清單? – 2012-07-27 15:12:13

+0

我已經爲您提供了標準Magento安裝中存在的所有自定義頁面佈局句柄的列表。 – 2012-07-27 15:12:59

+0

所有其他頁面通常使用它們。也可以將此更改添加到''手柄中。 – 2012-07-27 15:14:21

1

作爲Ivans答案的替代方案,您還可以使用事件觀察器來設置模板。有關更長的解釋,請參閱this answer。這是你可以使用代碼:

應用程序/設計/前端/ yourpackage/yourtheme /佈局/ local.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<layout version="0.1.0"> 

    <!-- your other adjustments for default, category_product_view and so on go here --> 

    <set_root_template> 
     <reference name="root"> 
      <action method="setTemplate"><template>your/template.phtml</template></action> 
     </reference> 
    </set_root_template> 
</layout> 

應用程序/代碼/ yourpool /公司/推廣/型號/ Observer.php

<?php 

    class Company_Extension_Model_Observer 
    { 
     /** 
     * Sets the template file for the root block. 
     * 
     * Uses the event 'controller_action_layout_load_before'. 
     * 
     * @param Varien_Event_Observer $observer 
     * @return YourCompany_YourExtension_Model_Observer 
     */ 
     public function setRootTemplate(Varien_Event_Observer $observer) 
     { 
      $layout = $observer->getEvent()->getLayout()->getUpdate(); 
      $layout->addHandle('set_root_template'); 
      return $this; 
     } 
    } 

應用程序/代碼/ yourpool /公司/擴展的/ etc/config.xml中

應用的/ etc /模塊/ Company_Extension。 xml

<?xml version="1.0" encoding="UTF-8"?> 
<config> 
    <modules> 
     <Company_Extension> 
      <active>true</active> 
      <codePool>local</codePool> 
     </Company_Extension> 
    </modules> 
</config> 
0

而作爲另一種選擇。 (但基本上與@MatthiasZeis相同)。我也只是提供一個模塊,這樣做)

我有同樣的問題,我想出了一個注射自定義手柄插入Magento的,用觀察者的事件同樣的想法。

因此我的代碼注入一個「負載最後」處理被叫<GLOBAL_OVERRIDE>,然後允許你設置通過佈局指令根模板。

因爲它加載最後,任何前一組佈局指令改變模板在<GLOBAL_OVERRIDE>

<GLOBAL_OVERRIDE> 
     <reference name="root"> 
      <action method="setTemplate"><template>page/2columns-left.phtml</template></action> 
     </reference> 
    </GLOBAL_OVERRIDE> 

替換爲一個可以很容易地使用它來全球範圍內取消另一柄,或取消孩子塊通過整個網站

我放在GitHub上的代碼:

https://github.com/ProxiBlue/GlobalHandle