Magento – what should be in type attribute of block

layoutmagento-1.8

I think I am confused because after going so further in learning magento, I am getting this basic doubt now.

The problem is that I created a new block in layout of my created theme folder. The thing which I can't understand is what should be the value of the "type" attribute of block layout element?

I know that "type" attribute refers to the php classes but what I can't understand is that which php class name should I assign as "type" attribute to the block layout element?

The block is as shown below with the "type" –> page/html_header. (I don't know why it is rendering fine with this "type" attribute value)

<block type="page/html_header" name="happy.label" as="happyLabel" template="page/html/happyLabel.phtml"/>

Here is the relevant xml layout code from design/frontend/default/{$mytheme}/layout/page.xml:

<default translate="label" module="page">
    <label>All Pages</label>
    <block type="page/html" name="root" output="toHtml" template="page/3columns.phtml">
        <!--------------->
        <block type="page/html_header" name="header" as="header">
            <block type="page/html_header" name="happy.label" as="happyLabel" template="page/html/happyLabel.phtml"/>
            <block type="page/template_links" name="top.links" as="topLinks"/>
            <block type="page/switch" name="store_language" as="store_language" template="page/switch/languages.phtml"/>
            <block type="core/text_list" name="top.menu" as="topMenu" translate="label">
                <label>Navigation Bar</label>
                <block type="page/html_topmenu" name="catalog.topnav" template="page/html/topmenu.phtml"/>
            </block>
            <block type="page/html_wrapper" name="top.container" as="topContainer" translate="label">
                <label>Page Header</label>
                <action method="setElementClass"><value>top-container</value></action>
            </block>
            <block type="page/html_welcome" name="welcome" as="welcome"/>
        </block>
        <!--------------->
    </block>
</default>

The template file just has five list items in UL and Li format.

EDIT:

  • How should I choose which value to pass to "type" attribute when creating a block?

Best Answer

You are correct that the type defines the block class that is used to render the display of your associated template file.

The reason why using 'page/html_header' is working, is because it is a child class of the base magento template block Mage_Core_Block_Template

class Mage_Page_Block_Html_Header extends Mage_Core_Block_Template

Since your template file is not calling methods not in the class Mage_Page_Block_Html_Header (or parents thereof), it will render fine, as ultimately you are dealing with the base core/template class which has all the needed base methods to allow a template to load, and render (for example protected function _toHtml())

You could just as well (and should have) used 'core/template' as your type, since you template required no special functionality to render.

Let say, for exmaple your template file had a call to a method to determine the answer to life, the universe, and everything :

<?php echo $this->whatIsTheAnswer() ?>

then your rendering of the template would have failed. $this is the type defined in the block definition, thus Mage_Page_Block_Html_Header in your case/example.

Thus type = the class that contains your methods, as called from your template file. ($this in the template) If you have no custom calls/methods in your template file, then core/template will suffice. If you need custom methods, then you need to create that block, in your custom module, and use it as the type, but your custom block must ultimately lead to 'Mage_Core_Block_Template' via inheritance.

I hope that helps you understand it a bit more, if not, ask away and I will try and explain more.

Related Topic