Thursday 29 October 2015

Layout Element Block : Attributes & Action Method Reference List


Inside the handle there is the different blocks which specify the template file to be render when that block is being called. Blocks are of two types:
  • Structurer Blocks  
  • Content Blocks
In our layout file we are defining only the Content Blocks and then we wrap them in Structurer Blocks. For example, if someone is calling a customer registration page and we want to load it on left, right, content, or footer, we wrap that block in its respective structurer block. Here, we have wrapped two blocks inside the "content" block which is a structurer block.

<Block>
This element is used to define a new block. The element is usually defined within the reference element, when we want to create a new block. The block element must have two attributes: name and type. Name is a unique identifier of the block in the layout while the type defines the block class name. If the block is of type or subtype of core/template, it can also have the template attribute which defines the actual phtml template file to be used for rendering the block.

The attributes of the <block> element

Blocks contains attributes like following:

type this is the ID of the module class. It defines the functionality of the block.
name - this is a unique block name, which is needed to refer to with the help of reference.
as - this is the alias for the structural block. It is required when we need to refer to the block code via such parent block methods as getChild or getChildHtml. If the getChildHtml(‘block_name’) PHP method is called from a template, than it is definitely referring to the block whose attribute ‘as‘ is assigned the name ‘block_name’. In the file  app/design/frontend/base/default/layout/page.xml. you can find such structural blocks as “left”,”right”,”content “,”footer”.
before  this one is used to position block content in a structural block. The name of the block, before which it is required to output this block is defined. If you specify the minus sign ( ” – ” ), the block will be output the first among others in its parent.
after  is used to position block content in a structural block.  after=”-” indicates the name of the block after which this block should be output.
template - This attribute defines the template which will represent block functionality, in which this attribute is set.
translate indicates tags which should be translated.
for example file app/design/frontend/base/default/layout/page.xml
<block type="core/text_list" name="after_body_start" as="after_body_start" translate="label">
    <label>Page Top</label>
</block>
action  <action> allows you to invoke a block method and transmit parameters into it directly from the markup file. For example the file app/design/frontend/base/default/layout/page.xml the method attribute addJs, addCss, addItem is applied

<reference>
The <reference> tag is use to extend the blocks that already exist. In this case, we have extended the content block and inserted our own block into it. You must use the correct block name that you want to extend.

<remove>
The <remove> tag is use to remove the specific block. For example, say that on your account registration page you, don't want to display the right and left columns. In that case, then you can simply remove that block with the following syntax <remove name="your block name">.

Child <block>
When you wrap one block under another block, that wrapped block is called child block. Whenever our parent block is called by our module, the child block is automatically called.
         
<block type='core/template' name='parent' template='parent.phtml'>
<block type='core/template' name='child' template='child.phtml'/>
</block>

You can call child block separately too with below syntax in you template file
echo $this->getChildHtml(‘child');

root <block>

Open page.xml layout file, you will find the <root> block that is looks like below
         
<block type="page/html" name="root" output="toHtml" template="page/3columns.phtml">

Magento starts rendering from the root block. All other blocks are child blocks of the root block. The root block defines the structure of the page. Here, you can see that currently it's set to 3columns.phtml, you can change this to 1column.phtml, 2columns-right.phtml or 2columns-left.phtml.

Action Method Reference List:

<action> allows you to invoke a block method and transmit parameters into it directly from the markup file. Below are the list of these methods with a bit more details.

Mage_Core_Block_Abstract

unsetChild($alias) - Unset child block.

Parameter
Name
Default
Value
Description
Alias
N/A
Alias of the child block that is to be unset.

unsetChildren() - Unset all children blocks.

append($block, $alias = ’’) - Append child block. This function calls insert ($block, ‘’, true, $alias)

Parameter
Name
Default
Value
Description
Block
N/A
Name of the block that is too be inserted.
Alias (optional)
‘’
Alias that is to be given to the block

insert($block, $siblingName = ”, $after = false, $alias = ”) - Insert child block.

Parameter Name
Default Value
Description
Block
N/A
Name of the block that is too be inserted.
Sibling Name (optional)
‘’
Inserts block before or after this child (if found). Before or after is dependent on the value of <after /> parameter
After (optional)
False
If a sibling name is set, this controls whether the block is inserted before or after the sibling.
If no sibling is set, this controls whether the block is added to the very start or very end of the list of children.
Alias (optional)
‘’
Sets an alias for the block

addToParentGroup($groupName) - Add self to the specified group of parent block.

Parameter
Name
Default
Value
Description
Group Name
N/A



Mage_Core_Block_Template

setTemplate($template) - Set path to template used for generating block’s output.

Parameter
Name
Default
Value
Description
Template
N/A
Path to the template file.

Mage_Page_Block_Html

setHeaderTitle($title)

Parameter
Name
Default
Value
Description
Title
N/A
Sets the title of the page.

Mage_Page_Block_Html_Head

addCss($name, $params = ‘’) - Add CSS file to HEAD entity. This function calls

Parameter
Name
Default Value
Description
Name
N/A
Location of the CSS file to be added within the skin directory.
Params (optional)
‘’


addJs($name, $params=’’) - Add Javascript file to HEAD entity. This function calls addItem(‘js’, $name, $params)

Parameter Name
Default Value
Description
Name
N/A
Location of the javascript file to be added within the js directory.
Params (optional)
‘’


addCSSIe($name, $params=’’) - Add CSS file for Internet Explorer only to HEAD entity. This function calls addItem(‘skin_css’, $name, $params, ‘IE’)

Parameter
Name
Default Value
Description
Name
N/A
Location of the CSS file to be added within the skin directory.
Params (optional)
‘’


addJsIe($name, $params=’’) - Add JavaScript file for Internet Explorer only to HEAD entity. This function calls addItem(‘js’, $name, $params, ‘IE’)

Parameter Name
Default Value
Description
Name
N/A
Location of the javascript file to be added within the js directory.
Params (optional)
‘’


addLinkRel($rel, $href) - Add Link element to HEAD entity. This function calls addItem(‘link_rel’, $href, ‘rel=”‘ . $rel . ‘”‘)

Parameter
Name
Default
Value
Description
Rel
N/A

Href
N/A


addItem($type, $name, $params = null, $if = null, $cond = null) - Add HEAD Item.

Parameter Name
Default Value
Description
Type
N/A
Allowed types: js, js_css, skin_js, skin_css, rss
Name
N/A

Params (optional)
null

If (optional)
null
If set, this is will be used to wrap around your file. Good for including files for certain browsers only.
Example: lt IE 7
Cond (optional)
null


removeItem($type, $name) - Remove Item from HEAD entity.

Parameter
Name
Default
Value
Description
Type
N/A
Allowed types: js, js_css, skin_js, skin_css, rss
Name
N/A


setTitle($title) - Set title element text.

Parameter
Name
Default
Value
Description
Title
N/A



Mage_Catalog_Block_Product_View_Options

addOptionRenderer($type, $block, $template) - Add option renderer to renderers array.

Parameter
Name
Default
Value
Description
Type
N/A
Type of option being added.
Example Types: text, file, select
Block
N/A

Template
N/A


Mage_Catalog_Block_Seo_Sitemap_Abstract

bindPager($pagerName) - Init pager.

Parameter
Name
Default
Value
Description
Pager Name
N/A



Mage_Page_Block_Template_Links

addLink($label, $url = ‘’, $title = ‘’, $prepare = false, $urlParams = array(), $position = null, $liParams = null, $aParams = null, $beforeText = ‘’, $afterText = ‘’) - Add link to the list.

Parameter
Name
Default
Value
Description
Label
N/A

Url (optional)
‘’

Title (optional)
‘’

Prepare (optional)
False

urlParams (optional)
Array()

Position (optional)
Null

liParams (optional)
Null

aParams (optional)
Null

beforeText (optional)
‘’

afterText (optional)
‘’


Mage_CatalogSearch_Block_Result

setListOrders() - Set search available list orders.
setListModes() - Set available view mode.
setListCollection() Set Search Result collection.

Mage_Catalog_Block_Product_List_Upsell

setColumnCount($columns) –

Parameter
Name
Default
Value
Description
Columns
N/A


setItemLimit($type, $limit) - Set how many items we need to show in upsell block Notice: this parameter will be also applied

Parameter Name
Default
Value
Description
Type
N/A

Limit
N/A


Let me know in comment if this has helped you ...
Happy Coding .... :) ....