4

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?

1 Answer 1

7

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.

3
  • yeah your explanation makes sense to me :). I was assuming the same but as I am not from background of PHP, I thought I can evade it by using some alternating things. but it seems php must be used if I am willing to have my own method. Thanks.
    – Mr_Green
    Oct 9, 2013 at 8:57
  • Fantastic answer, really help me understand this, thankyou!
    – Tom Burman
    Apr 24, 2014 at 21:49
  • 1
    @TomBurman My Pleasure. Wish I had more time these days to help out further, but since my daughter had entered toddler-dom (hood?) I find no free time to spend on stack exchange :(
    – ProxiBlue
    Apr 26, 2014 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.