Wednesday 28 October 2015

Magento Block & Built-in Types



There are two types of blocks at the end of the day - those that render automatically and those that don't.

Auto-rendered blocks
When defined in a layout, any block of type core/text_list will automatically render all its children. While core/text will automatically render itself it really only should contain text and therefore is not useful for layout purposes, but using those we can achieve cool things.

Other blocks
Any other block type will need to be rendered manually. Provide the block an alias which can then be passed to getChildHtml, returning the content which you then echo.

Essentially all you have to remember is that if you add an alias (the as tag) to your block you can render it by passing that alias as the first argument to getChildHtml.

For understanding more about Magento block types following are some built-in block types which are widely used in layout.

  • core/template: This block renders a template defined by its template attribute. The majority of blocks defined in the layout are of type or subtype of core/template. 
  • page/html: This is a subtype of core/template and defines the root block. All other blocks are child blocks of this block.
  • page/html_head: Defines the HTML head section of the page which contains elements for including JavaScript, CSS etc. 
  • page/html_header: Defines the header part of the page which contains the site logo, top links, etc.
  • page/template_links: This block is used to create a list of links. Links visible in the footer and header area use this block type. 
  • core/text_list: Some blocks like content, left, right etc. are of type core/text_list. When these blocks are rendered, all their child blocks are rendered automatically without the need to call the getChildHtml() method. 
  • page/html_wrapper: This block is used to create a wrapper block which renders its child blocks inside an HTML tag set by the action setHtmlTagName. The default tag is <div> if no element is set.
  • page/html_breadcrumbs: This block defines breadcrumbs on the page.
  • page/html_footer: Defines footer area of page which contains footer links, copyright message etc. 
  • core/messages: This block renders error/success/notice messages.
  • page/switch: This block can be used for the language or store switcher.

This is a list of only commonly used block types. Let me know in comment if this has helped you ...
Happy Coding .... :) ....

1 comment: