2013-06-05 63 views
21

如何使用佈局xml文件刪除一個已經存在的塊刪除塊?具體而言,我想從名爲「top.switches」的塊中刪除名爲「currency」的塊。它被插入在directory.xml文件,這樣的:Magento的 - 用更新XML

<default> 
    <reference name="top.switches"> 
     <block type="directory/currency" name="currency" before="store_language" template="directory/currency.phtml"/> 
    </reference> 
    <reference name="head"> 
     <block type="core/template" name="optional_zip_countries" as="optional_zip_countries" template="directory/js/optional_zip_countries.phtml" /> 
    </reference> 
</default> 

回答

36

有兩種方法通過另一個XML文件以去除在一個佈局XML文件中定義的塊,:

<default> 
    <reference name="top.switches"> 
     <action method="unsetChild"><name>currency</name></action> 
    </reference> 
</default> 

而這樣,你通常被期望做到這一點:

<default> 
    <reference name="top.switches"> 
     <remove name="currency" /> 
    </reference> 
</default> 

你可以找到不同的佈局XML元素here的解釋,但不包括可供操作的方法標籤。爲此,您需要查看塊類app/code/core/Mage/Core/Block/Abstract.php,其中包含unsetChild,unsetCallChild,insert,sortChildren等各種有用函數。

8

在佈局目錄中添加名爲local.xml的文件。然後在local.xml中,您可以使用「刪除」標籤刪除任何塊。 BTW刪除標籤應該是「佈局」和「默認」 之間,然後將該文件應該是:

<?xml version="1.0" encoding="UTF-8"?> 
<layout> 
    <default> 
    <remove name="BLOCK_NAME" /> 
    </default> 
</layout> 
+0

我不認爲這個工程在Magento 2.0了 – CarComp

+1

@CarComp Magento的1和2有完全不同的平臺非常不同的方法來佈局XML,所以你會是正確的。 – Navarr

+0

@CarComp你會很驚訝於如此相像M1和M2有關remove元素:http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/xml-instructions.html –