What is the difference between remove and unsetChild methods in layout?
For example (in poll.xml layout file):
<customer_account_index> <reference name="right"> <action method="unsetChild"><name>catalog_compare_sidebar</name></action> </reference> </customer_account_index>
Why unsetChild and not just remove?
Remove nodes will be processed after all layout handles are merged, and are a good way to remove a block regardless of which layout handle loaded the block; you just want to get rid of it entirely for some handles! It also removes recursively, so all you need to specify is the layout handle.
On the other hand, you may only want to remove a block from a reference in a specific layout handle, in which case you should use unsetChild. It is often used to remove a block from a reference, but then re-insert the same block with a different position. This would not have been possible with remove.
In your very specific example, the magento developers used it to give magento some flexibility. Let's say I added a subpage for the account index page, and the following layout handles were loaded:
And now assume on this subpage I would actually want the 'catalog_compare_sidebar' block. If they had used 'remove', I would not be able to add this block (with this specific name) because 'remove' would be processed -after- I had added the block myself.
This allows you to easily make changes from one single file; local.xml.
Taking your code as an example,if you use unsetchild
,compare sidebar block removed from right column,but you can use it anywhere like left column,footer etc..,remove
removes entirely from the template,and cannot be used anywhere.
Remove the compare sidebar using remove
and if you call it somewhere else,error will be thrown.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With