Blocks

http://www.danielnavarroymas.com/magento-certification-rendering-blocks-part-1/

##Describe the programmatic structure of blocks:

###What are blocks used for in Magento?

Blocks make up the structural contents of a page/route in Magento by recursively calling $this->getChildHtml(). All blocks extend from the root block, however this is a block itself. Each block contains a bunch of methods which helps with the page rendering and getting data from your models.

The best way to visually think about blocks in a structural sense for myself is they are similar to the div in the way they make up parts of the page, such as the header, left bar, body ect.

Please take this analogy with a pinch of salt, as obviously the head of a HTML page isn’t visible or a div however it is a block. However I visualise the two very different objects in similar ways.

###What is the parent block for all Magento blocks?

Mage_Core_Block_Abstract

###Which class does each block that uses a template extend?

Mage_Core_Block_Template

###In which way does a template block store information about its template file? Does it store an absolute or a relative path to the template?

The template block class has a the functions getTemplate() and setTemplate(). These functions use a relative path:

  • app/design/{area}/{package}/{theme}/template/{parameter}

You always call a template relative to the template folder.

###What is the role of the Mage_Core_Block_Abstract class?

This holds basic functionality methods used in all Block instances, such as:

  • toHtml() - This is a final function meaning it cannot be re-written.
  • getChild()
  • getChildHtml()

These code references can be used as an entry point to find answers to the questions above:

  • Mage_Core_Model_Layout
  • Mage_Core_Model_Layout_Update
  • Mage_Core_Block_Template
  • Mage_Core_Block_Abstract
  • Mage_Adminhtml_Block_Abstract

##Describe the relationship between templates and blocks:

If your block is an instance of Mage_Core_Block_Template you can set a template. When rendering content Magento calls Mage_Core_Block_Abstract::toHtml() which in turn calls the protected function $this->_toHtml(). Magento’s Mage_Core_Block_Template template class has this function in, it checks to see if a template is set and if it is it renders it.

Within your template file you can have access to the functions defined within your block by using $this.

###Can any block in Magento use a template file?

In theory yes because you could override the _toHtml function and use a template, however in practice only a block which is an instance of Mage_Core_Block_Template can use a template.

###How does the $this variable work inside the template file?

As mentioned before $this is the class of the block you are using. The reason you have access to this in your template file is because Magento uses output buffering while loading the templates.

You can see this ob_start() method being called in Mage_Core_Block_Template::fetchView() method, it’s a good one to look at to understand tempting in Magento.

###Is it possible to render a template without a block in Magento?

Not without breaking convention; you could like with anything hack it but it’s not recommended.

###Is it possible to have a block without a template in Magento?

Yes, the only block type which actually requires a template is Mage_Core_Block_Template, and even with this you could just override the protected _toHtml() method to get an output… but I cannot see any reason why you’d ever do this.

These code references can be used as an entry point to find answers to the questions above:

  • Mage_Core_Block_Template
  • Mage_Core_Block_Abstract

##Describe the stages in the lifecycle of a block:

  • Blocked declared in layout or just code (from the layout object) - This happens in your controller when $this->loadLayout() is called
  • Block instance created in in it’s _prepareLayout() method.
    • This happens in your controller when $this->loadLayout() is called.
    • A setup function
  • renderLayout() method is called
    • This happens in your controller when $this->renderLayout() is called.
    • Will call the Mage_Core_Block_Abstract::toHtml() method.
  • _ beforeToHtml(),_ toHtml() and _ afterToHtml are called.
    • Can be used for modifying output or logging.

###Which class is responsible for creating an instance of the block?

Mage_Core_Model_Layout::createBlock()

###Which class is responsible for figuring out which blocks should be created for certain pages?

Mage_Core_Model_Layout_Update references which blocks fall under which layout handles.

###How is the tree of blocks typically rendered?

Within your controller $this->renderLayout();.

###Is it possible to create an instance of the block and render it on the page without using the Magento layout?

Yes but only through hacking:

	$block = new Mage_Core_Block_Text()->setText('hello');
$this->getResponse()->appendBody($block->toHtml();

So the answer is no, not really.

###Is it possible to create an instance of the block and add it to the current layout manually?

Yes, you can add a block using the following code:

	$block = Mage::app()->getLayout()->createBlock({module}/{block})
                    ->setName({name})
                    ->setTemplate({template});
Mage::app()->getLayout()->getBlock('content')->append($block);

It is recommended you only do things like this in your controller, or perhaps an observer. If you are in a controller you have access the the layout object by using $this->getLayout().

###How are a block’s children rendered? Once you added a child to the block, can you expect it will be rendered automatically?

It depends on the blocks parent, if it’s parent is a text_list block it will be rendered automatically, however if it’s parent is a template type it will need to have a $this->getChildHtml() call. I recommend looking at the _toHtml method on all block types.

###What is a difference in rendering process for different types of blocks?

These code references can be used as an entry point to find answers to the questions above:

  • Mage_Core_Model_Layout
  • Mage_Core_Model_Layout_Update
  • Mage_Core_Block_Template
  • Mage_Core_Block_Abstract
  • Mage_Core_Block_Text
  • Mage_Core_Block_Text_List

##Describe events fired in blocks:

  • core_block_abstract_to_html_before
  • core_block_abstract_to_html_after

###How can block output be caught using an observer?

The events mentioned above pass in $this as a parameter which means which means we can modify the block either before toHtml is called or after.

###What events do Mage_Core_Block_Abstract and Mage_Core_Block_Template fire?

These code references can be used as an entry point to find answers to the questions above:

  • Mage_Core_Block_Abstract

##Identify different types of blocks:

###What is the purpose of each of the following block types:

  • Mage_Core_Block_Template
    • This is for defining blocks which use templates and need structure
  • Mage_Core_Block_Text_List
    • This block automatically calls all of it’s child blocks and outputs them
  • Mage_Core_Block_Text
    • This is a very simple block which you can call setText() on and it will render itself automatically

###Which block type renders its children automatically?

  • Mage_Core_Block_Text_List

###Which block type is usually used for a “content” block on Magento pages?

Mage_Core_Block_Text_List.

These code references can be used as an entry point to find answers to the questions above:

  • Mage/Core/Block/*
  • Mage_Core_Block_Abstract
  • Mage_Core_Block_Template
  • Mage_Core_Block_Text
  • Mage_Core_Block_Text_List
  • Mage_Page_Block_Html_Head

##Describe block instantiation:

###How can a template’s block instance be accessed inside the template file, and how can other block instances be accessed?

$this

###How can block instances be accessed from the controller?

    $this->getLayout->createBlock({block_instance})

###How can block instances be accessed inside install scripts or other model class instances?

These code references can be used as an entry point to find answers to the questions above:

  • Mage_Core_Model_Layout
  • Mage_Core_Model_Layout_Update
  • Mage_Core_Controller_Varien_Action

##Explain different mechanisms for disabling block output:

###In which ways can block output be disabled in Magento?

You can do this in configuration XML

###Which method can be overridden to control block output?

These code references can be used as an entry point to find answers to the questions above:

  • Mage_Core_Model_Layout

##Describe how a typical block is rendered:

`Mage_Core_Block_Abstract::toHtml`

is call, which in turn calls the protected function _toHtml() this function is usually executed in the block type in question and prepares the content.

###Which class performs rendering of the template?

Mage_Core_Block_Template

###Which classes are responsible for figuring out the absolute path for including the template file?

`Mage_Core_Block_Template::getTemplateFile()`

which in turn calls

`Mage_Core_Model_Design_Package::getTemplateFilename`

.

###In which method are templates rendered?

Mage_Core_Block_Template::fetch()

###How can output buffering be enabled/disabled when templates are rendered?

By setting

`$this->getLayout()->getDirectOutput()`

.

These code references can be used as an entry point to find answers to the questions above:

  • Mage_Core_Model_Layout
  • Mage_Core_Model_Layout_Update
  • Mage_Core_Block_Template
  • Mage_Core_Block_Abstract
comments powered by Disqus