Understanding Magento Block and Block Type
Asked Answered
D

5

106

I just want to understand the meaning of

 <block type="page/html" name="root" output="toHtml" template="example/view.phtml">

I got many references from Google and understood many things about it but I am still unable to understand the formation of type="page/html" how to form a type for my custom module.

Please explain

type="A/B"

Let me know where this A and B come from?

Deforce answered 9/7, 2011 at 7:8 Comment(0)
R
126

The A is a module's alias. In this case page is short for Mage_Page_Block (it is defined in app/code/core/Mage/Page/etc/config.xml if you want to see).

The B is the class name relative to the alias, initial letters of each word are capitalised. In this case html becomes Html and is appended to the resolved alias, so it is Mage_Page_Block_Html. This is probably found in the file app/code/core/Mage/Page/Block/Html.php because class names translate directly to locations in Magento.

Were you using a model alias instead of a block alias then page would be Mage_Page_Model instead. The same thing happens for resource models and helpers too. Your own module will need to define these in it's config if it is to have blocks, models and helpers.

Rippy answered 9/7, 2011 at 13:8 Comment(3)
Nice ExplainationRoth
What if it is a rewrited/overrided block, how can I define and use block type then ?Appalling
@VickyDev you don't need to do anything different as the new block will be a descendant of the old block and so is compatible. You can, and should, pretend it doesn't even exist.Rippy
H
158

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

  1. 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.
  2. page/html: This is a subtype of core/template and defines the root block. All other blocks are child blocks of this block.
  3. page/html_head: Defines the HTML head section of the page which contains elements for including JavaScript, CSS etc.
  4. page/html_header: Defines the header part of the page which contains the site logo, top links, etc.
  5. 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.
  6. core/text_list: Some blocks like contentleftright etc. are of type core/text_list. When these blocks are rendered, all their child blocks are rendered automatically without the need to call thegetChildHtml() method.
  7. 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.
  8. page/html_breadcrumbs: This block defines breadcrumbs on the page.
  9. page/html_footer: Defines footer area of page which contains footer links, copyright message etc.
  10. core/messages: This block renders error/success/notice messages.
  11. page/switch: This block can be used for the language or store switcher.

This is a list of only commonly used block types. There are many other block types which are used in advanced theme implementations.

Hildehildebrand answered 2/8, 2012 at 5:39 Comment(3)
What about : {{ block type="cms/block" block_id="terms" }}Poussette
@Poussette Already mentioned in answer that :This is a list of only commonly used block types. There are many other block types which are used in advanced theme implementations.Hildehildebrand
Thanks. Was really helpful.Monody
R
126

The A is a module's alias. In this case page is short for Mage_Page_Block (it is defined in app/code/core/Mage/Page/etc/config.xml if you want to see).

The B is the class name relative to the alias, initial letters of each word are capitalised. In this case html becomes Html and is appended to the resolved alias, so it is Mage_Page_Block_Html. This is probably found in the file app/code/core/Mage/Page/Block/Html.php because class names translate directly to locations in Magento.

Were you using a model alias instead of a block alias then page would be Mage_Page_Model instead. The same thing happens for resource models and helpers too. Your own module will need to define these in it's config if it is to have blocks, models and helpers.

Rippy answered 9/7, 2011 at 13:8 Comment(3)
Nice ExplainationRoth
What if it is a rewrited/overrided block, how can I define and use block type then ?Appalling
@VickyDev you don't need to do anything different as the new block will be a descendant of the old block and so is compatible. You can, and should, pretend it doesn't even exist.Rippy
D
8
<block type="page/html" name="root" output="toHtml" template="example/view.phtml">

page is a FrontendName defined in etc/config.xml file html is a block class name

more details:

in this line type(type="page/html") define block class name related to your template(template="example/view.phtml">) and name is the unique for each block.

first see folder structure

app>local>namespace>modulename>etc>config.xml

we set FrontendName = 'mymodule'

app>local>namespace>modulename>Block>hello.php

in hello.php you created a function

   class namespace_modulename_Block_Data extends Mage_Core_Block_Template
   {
    public function mydata()
        {
            $data = "Block is called";
            return $data;
         }     
}   

and now come to your layout xml page:

<block type="mymodule/data" name="xyz" template="example/view.phtml"> here mydata is frontend name

and now come to your template's

template/example/view.phtml page

here you can call directly mydata() function

like

<div>
<?php echo $this->mydata(); ?>
</div>

now you can get your output in browser "Block is called"

Digamma answered 1/5, 2015 at 14:44 Comment(1)
You named the block hello.php but the name of the block class is 'namespace_modulename_Block_Data' , should'nt it be Hello at the end of the name?Menander
K
5

i don't know about "B" type, but "A" refer to your module name tag in config.xml example in config.xml:

<A><!-- script --></A>

No, you're not wrong. But the "<!-- script -->" may be confusing. Let's clarify: as the above answers stated, this is an alias, which consisted of two parts, the first part ("A") itself is the alias you define to your module's classes in your module's config.xml, the second is a path relative to the node's value. These together ("A" + capitalized "B") will be translated to a class name, using the first part (the "A" node's value) exactly as you define it (watch for upper/lowercase if you don't want hours of suffering) and the second part is capitalized after each underscore. So, start with the A/B example with a block and this config:

<config>
  ...
  <global>
    <blocks>
      <A>Vendor_Module_Block</A>
    </blocks>
  </global>
  ...
</config>

In runtime A/B would resolved by the Magento config to Vendor_Module_Block_B which would then included by the autoload from the following path: public/app/local/Vendor/Module/Block/B.php. To get a better understanding I advise you to take a look at the Mage_Core_Model_Config::getGroupedClassName(), Mage_Core_Model_Config::getModelInstance() and Varien_Autoload::autoload().

Kamenskuralski answered 18/3, 2013 at 9:5 Comment(0)
A
3

I don't know about "B" type, but "A" refer to your module name tag in config.xml

An example in the config.xml file:

<A><!-- script --></A>

nb: i hope i'm not wrong..

Affettuoso answered 9/7, 2011 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.