Saturday, April 7, 2012

The Better Way to Modify Magento Layouts

Here is the slimmed down, commented local.xml from one of our recent projects:
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
 <reference name="head">
  <!-- Magento looks in /skin/frontend/<INTERFACE>/<THEME>/js/buyprinting.js
  for this file -->
  <action method="addItem"><type>skin_js</type><name>js/buyprinting.js</name></action>
  <!-- This removes the item that was set in the page.xml file -->
  <action method="removeItem"><type>skin_js</type><name>js/iehover-fix.js</name></action>
  <!-- Magento looks in /js/prototype/element.storage.js for this file -->
  <action method="addJs"><name>prototype/element.storage.js</name></action>
  <action method="addCss">
<stylesheet>css/buyprinting.css</stylesheet></action>
 </reference>
 <reference name="header">
        <!-- This adds a CMS block that can be called from the template file
        associated with the header block. -->
  <block type="cms/block" name="cms_quick_help">
   <action method="setBlockId"><block_id>quick_help</block_id></action>
  </block>
        <!-- The remove tag removes the blocks with the specified name from the layout -->
  <remove name="top.menu"/>
  <remove name="store_language"/>
  <remove name="breadcrumbs"/>
 </reference>
 <reference name="top.nav">
  <remove name="catalog.topnav"/>
 </reference>
 <reference name="left">
  <remove name="left.newsletter"/>
  <remove name="left.permanent.callout"/>
  <remove name="catalogsearch.leftnav"/>
        <!-- When you use the remove tag, it removes any blocks with the specified name from
            the entire layout, regardless of the context. So, if I remove right.newsletter in
            the <default> context and that name is used in say the <catalog_product_view> context,
            then both blocks will be removed.  Because remove operates on the global context,
            you can only remove an element once.  Since <remove name="right.newsletter" /> is
            being called in catalogsearch.xml, we have to unset it, or else we'll get an error.

            The line below only unsets the block from the parent's context, not the global
            layout context -->
  <action method="unsetChild"><name>right.newsletter</name></action>
 </reference>
 <reference name="right">
        <!-- Some blocks have to be removed using remove, others via unsetChild.
            I've not spent the time digging into the code to figure out why -->
  <remove name="right.permanent.callout"/>
  <remove name="catalog.compare.sidebar"/>
  <remove name="left.reports.product.viewed"/>
  <action method="unsetChild"><name>sale.reorder.sidebar</name></action>
  <action method="unsetChild"><name>wishlist_sidebar</name></action>
  <action method="unsetChild"><name>right.reports.product.viewed</name></action>
  <remove name="cart_sidebar"/>
 </reference>
</default>

<!-- CATALOG PAGES -->
 <catalog_product_view><!-- 2columns-right -->
  <reference name="root">
   <action method="setTemplate"><template>page/2columns-left.phtml</template></action>
  </reference>
  <reference name="content">
   <reference name="product.info">
    <block type="cms/block" name="cms_product_info_tabs">
     <action method="setBlockId"><block_id>product_info_tabs</block_id></action>
    </block>
    <block type="catalog/product_view" name="product.clone_prices" as="prices" template="catalog/product/view/price_clone.phtml"/>
    <action method="unsetChild"><name>tierprices</name></action>
    <action method="unsetChild"><name>addto</name></action>
    <remove name="addto"/>
    <reference name="product.info.options.wrapper.bottom">
     <action method="unsetChild"><name>product.tierprices</name></action>
    </reference>
   </reference>
  </reference>
 </catalog_product_view>
</layout>

Other:
<!-- Append the cart_sidebar block into the right block -->
   <reference name="right">
      <action method="append"><block>cart_sidebar</block></append>
   </reference>

   <!-- Remove the cart_sidebar block from the left sidebar -->
   <reference name="left">
      <action method="unsetChild"><block>cart_sidebar</block></action>
   </reference>

1 comment:

  1. ooh great way how to modify the magneto , actually it was very easy to work on the back end but very tough to work on front end.and your giving nice way of explaining that.

    ReplyDelete