Magento Block Design analysis (in-depth analysis)

Source: Internet
Author: User

Block in Magento is a very important component that plays a very important role in block, so let's analyze how block is designed in Magento and how we should use this important role.
1, Magento block class diagram:
Figure 1:

Objects that inherit varien_object have a _data class variable that collects data in the form of a set property name (property value), such as: Setcustomerid (4) or SetData (property name, property value) such as: SetData (' Customer_ID ', 4), the above two forms of meaning, but pay attention to the different names of properties.

2. Classification of Magento block

As can be seen from Figure 1, block in succession is clearly divided into two branches, a branch for the user to inherit Mage_core_block_template custom Block, a branch for the Magento system are our custom Mage_core_block_ Text, generally we no longer inherit the extension.

categorize from a template file

Block with no template file. This is the class block, also known as the Pure container block (the right part). This type of block does not require a template file, such as left, content, etc. defined under the default node in Page.xml. He can only contain sub-blocks. He collects all the direct child HTML as his own output. Use the following statement in the template file of his parent block for output such as: <?php echo $this->getchildhtml (' left ')?> and <?php in 3columns.phtml: echo $this->getchildhtml (' content ')?> output HTML content in his own place.
Block (the left part of Figure 1) must have a template file. Blocks under this type can be divided into two categories:
The first Category: container block, this type of block has its own template file, that is, has its own HTML content, this part of the content can be layout or direct content, while this type of block includes sub block, In its own template file, you can use the <?php echo $this->getchildhtml (' alias of sub-block ')?, the contents of the output sub-block, As part of the content of the self template file.
Second class: pure content block, this type of block has its own template file, that is, has its own HTML content, but HTML can only have direct content, it does not contain sub-block, that is, the block is a leaf node.

Press whether the CMS block is divided.

Figure 2:

The color of the image is the CMS block, the white part of the non-BLOCK.CMS block is actually a block that does not require a template file, but it cannot contain sub-blocks. It is generally used in two ways:
Used in XML:

1 <  type name as = "Testcmsblock" > 2  <action method= "Setblockid" ><block_id>cms_block_id</block_id></action> 3 </block >

This output is then in the template file of his parent block: <?php Echo $this->getchildhtml ('testcmsblock')?>
Use it directly in your PHP code:

$cmsBlock = Mage::getSingleton(‘core/layout’)->createBlock(‘cms/block’, ‘test_cms_block’)->setBlockId(‘cms_block_id’);$cmsBlock->toHtml()

Sort by whether output outputs are available

All blocks have a tohtml method that makes their own HTML output, and he calls the template file to generate HTML content, if there is echo $this in the template content The getchildhtml statement will call his child block for content output and place it where he put the Echo $this->getchildhtml statement, and the child block will call its own template file to generate HTML content. If there is an echo $this->getchildhtml statement in the template content, it will call his child block for content output and place it where he put the echo $this->getchildhtml statement ... Until there is no child block content, that is, pure content block. That is, as long as the topmost block calls the Tohtml method, it will have a series of reactions, and eventually generate a finished HTML output, then who is the topmost block, it is defined in the Page.xml <block type= "page/html" Name= "root" output= "toHtml" template= "page/3columns.phtml" > this block. We found this block, there is a output= "toHtml", At the same time his module file is a full HTML page. None of the other blocks have this sentence. (Refer to << How to use and extend the interpretation of the layout directive in Directives >> in the Cms/email template), you can also <block type= "core/text_list" name= "Content" as= " Content "output=" toHtml "/> Add an output to the content to see what is happening in the foreground (the contents block will output two times). Remember: A layout instance will only have an output definition in the top block, or there will be a problem with multiple outputs.

Sort by front background

Front and back office (Admin) are user-defined block, but because the background is different, all the blocks of the backend module inherit Mage_adminhtml_block_template, the front block is inherited Mage_core_block_ Template

The following analysis, if not specifically described, refers to a block with a template file

4. Block design from the MVC architecture level

Figure 3:

As can be seen from Figure 3, in block design:
M: Used by model and helper, the block class calls the business processing method to these classes. These classes interact with the database (helper wants to interact with the database and is also implemented through model)
V: Used by the template file, which is the phtml file, he will use $this to invoke the method in block to get the data and convert it into HTML.
C: By the class of block, the class in block will call M to get the data.
The VC two parts of block are part of the module, they are part of the block, but one is used to get the data, one to display the data. But physically they belong to different documents. In the Fetchview method in Mage_core_block_template, there is a sentence stating this: include $this->_viewdir.ds. $fileName; This means that the block class file will include its own template file, which is assembled into a whole, which is why the method in the block class can be called $this in the module file. Sometimes I deliberately divide blocks into block files and module files, which helps us understand block design.

Magento Block Design analysis (in-depth analysis)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.