Studyon Minte9.com
Magento




Layout & Blocks



The "V" in Magento's MVC already differs from what you're probably used to, in that you need to explicitly kick off 
rendering the layout. The Controller action ends with two calls:
	
	$this->loadLayout();
	$this->renderLayout(); 

A Magento Layout is an object that contains a nested/tree collection of "Block" objects. Each Block object will
 render a specific bit of HTML. Blocks objects are meant to interact with the Magento system to retrieve data from 
Models, while the phtml template files will produce the HTML needed for a page.

For example, the page header Block 

	app/code/core/Mage/Page/Block/Html/Head.php users
	page/html/head.phtml

Another way of thinking about it is the Block classes are almost like little mini-controllers, and the .phtml files are 
the view.

To add Content to this Layout you need to tell the Magento system.

	public function indexAction() {
	    $block = $this->getLayout()->createBlock('adminhtml/system_account_edit')
	    $this->getLayout()->getBlock('content')->append($block);
	} 

But more commonly (at least in the frontend cart application), is use of the XML Layout system.

The Layout XML files in a theme allow you, on a per Controller basis, to remove Blocks that would normally be 
rendered, or add Blocks to that default skeleton areas. For example, consider this Layout XML file: 

	<catalog_category_default>
	    <reference name="left">
		<block type="catalog/navigation" name="catalog.leftnav" after="currency" 
                           template="catalog/navigation/left.phtml"/>
	    </reference>    
	</catalog_category_default> 

It's saying in the catalog Module, in the category Controller, and the default Action, insert the the catalog/navigation
 Block into the "left" structure Block, using the catalog/navigation/left.phtml template.

This is how a Block renders a nested Block.
	$this->getChildHtml('order_items');

If, however, we had something like:
	<catalog_category_default>
	    <reference name="left">
		<block type="catalog/navigation" name="catalog.leftnav" after="currency" 
                             template="catalog/navigation/left.phtml">
		    <block type="core/template" name="foobar" template="foo/baz/bar.phtml"></block>
		</block>
	    </reference>    
	</catalog_category_default>

From the catalog/navigation Block, we'd be able to call

	$this->getChildHtml('foobar');


http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-1-introduction-to-magento#7