Magento development note 3

Source: Internet
Author: User
In this section, let's take a look at Layouts and Blocks in the View. Unlike other mainstream PHPMVC architectures, magento's ActionController does not pass data objects to views or set properties in view objects. View is used to obtain the required information through the system module. This... SyntaxHigh let's take a look at Layouts and Blocks in the View. Unlike other mainstream PHPMVC architectures, magento's ActionController does not pass data objects to views or set properties in view objects. View is used to obtain the required information through the system module. The result of this design is that the View is divided into Blocks and Templates. Blocks is a PHP object, and Templates is a mix of PHP code and HTML (PHP can also be considered as a template language ). Each Block is bound to a Template file. In a Phtml file, the PHP keyword $ this will contain references to the corresponding Block of Temeplate. The following is a quick example. View the template file app/design/frontend/base/default/template/catalog/product/list. phtml and you will see the following code: GetLoadedProductCollection ()?> Count ():?> _ ("There are no products matching the selection.")?> The getLoadedProudctController can find the app/code/core/Mage/Catalog/block/Product/List in the corresponding Block file. php public functiongetLoadedProductCollection () {return $ this-> _ getProductCollection ();} among them, _ getProductCollection will instantiate models and get the data to the corresponding template. The real strength of built-in BlockBlocks/Templates is the getChildHtml method. This allows us to include the next-level Block/Template in the main Block/Template (xml format) Blocks calling Blocks will constitute our entire HTMLlayout. Let's look at an example App/design/frotend/base/default/template/page/1column. phtml GetChildHtml ('head')?> GetChildHtml ('content')?> GetChildHtml ('before _ body_end ')?> GetAbsoluteFooter ()?> This file is not long, but every call is $ this-> getChildHtml (...), This will include and render other blocks. These blocks may also call other blocks. Layout although the Block and Template are good, you may have the following questions: 1. how can I tell Magento which Block is used on the page? 2. how can we tell Magento is the initial 3. how can I tell each Block to call the following block? this requires the Layout object. The Layout object is in Xml format and defines which Blocks is contained in a page and which Blocks is used to render the page. In the previous Hello World project, we did this directly on the Action Method. This time we create a simple HTMLtemplate to serve this module. Create a file App/design/frontend/base/default/layout/local. xml and write the following content.  Create another file app/design/frontend/base/default/template/simple_page.phtml (note that it is consistent with the template in the configuration) and write the following content.  Untitled Body {background-color: # f00 ;}   Finally, the layout process is started in Aciton Controller. Add the following two lines of code: public functionindexAction () {// remove our previous echo // echo 'hello Index! '; $ This-> loadLayout (); $ this-> renderLayout ();} clear the cache and reload the Hello World controller page. you can see the background hi red on the page, the Html source code also corresponds to simple_page.phtml. What happened? Everything just now looks mysterious. Let's take a closer look at this process. First, we want to install the Layoutviewer module. This module is similar to Configviewer. Once installed, you can use the following URL http: // localhost/magento/helloworld/index? ShowLayout = page: a layout xml corresponds to the requested page. By And Tags. When you call the loadLaout method, 1. generate a Layout xml 2. instantiate And The following Block class. Find the tag with the tag name attribute, find the corresponding tag in the global configuration file, and store it in the internal_blocks array of the Layout object. 3. if The tag contains the output attribute, and its value is added to the internal_blocks array of the Layout object. In this way, when we call renderLayout in Action Controller, Mageno iterates all blocks in the _ Blocks array and uses the corresponding output attribute as the callback function. This is equivalent to the conversion to Html, which means that the Template of the Block is the starting point of the output. The following section describes how to instantiate a Block, how to generate a Layout file, and how to end the output. Block is instantiated in LayoutXml, Or There is a type equivalent to URI Mage_Page_Block  In this way, the MagePageBlock class is obtained. Then, the second part of URI becomes MagePageBlock_Html. This class will be instantiated later. Blocks is also a group class in Magento, and all share similar instantiation methods. This section will be detailed later. And We have talked about the difference. And Block can be instantiated. what is the difference between them. First available Then there are Blocks does not replace blocks. Instead, they add or modify the existing blocks. In the above example, a new root block is inserted into the existing rootblock. This is not defined in Magento Layout. The final result is that the old one is replaced, but it is a bad idea to ensure consistency. How to generate the Layout file so far we should have a clear understanding of Layout XML. But where does Layout XML come from? To answer this question, we need to introduce two new concepts: Handles and Package Layout. Handles Magento generates several different Handles for each request. The Layoutview module can display these values with URLs. http://localhost/magento/helloworld/index/index?showLayout=handles We will see a Handle similar to 1. default 2. STORE_bare_us 3. THEME_frontend_default_default 4. Helloworld_index_index 5. Customer_logged_out. Handle is set in many places of Magento. We need to pay attention to two of them: default and helloworld_index_index .. The default Handle is displayed for each request. Helloworld_index_indexHandle is composed of frontName (helloworld), Actioncontroller (index), and Action Controller Action Method (index. This means that each ActionController method may correspond to a Handle. Remember that "index" is Magento's default value for each Action Controller and ActionMethods. Therefore, the following request http://localhost/magento/helloworld/?showLayout=handles Similarly, Handle names are handed over to helloworld_index_index Package Layout. you can think that PackageLayout is equivalent to global configuration. It is a large XML file that contains every possible Layout configuration in Magento. Let's take a look. http://localhost/magento/helloworld/index/index?showLayout=package This may load for a while. If the browser is down in xml, switch to text mode. http://localhost/magento/helloworld/index/index?showLayout=package & ShowLayoutFormat = text you can see a large XML file. This is Package Layout. It combines all XMLLayout files under the current topic. The default installation method is app/design/frontend/base/default/layout/which has The node contains all the names to be loaded. Once the files in the configuration file are merged, Magento will be merged into the previous xml file, local. xml. This can increase the features you want. Merge Hanldes and Packge Layout. if you see Package Layout, you can see some familiar labels, such And But they all overwrite tags like this.   Etc... These are Handle labels. The Layout of a request is generated by the Package Layout of all Handles that match the request. Therefore, in the above example, our layout is generated in the following section         We need to pay attention to another tag. Allow us to include other Handle. For example This means that the request to customeraccountindex should contain The following And I have read enough theory. let's review the previous work.  This means that the root tag is overwritten. While This ensures that each request will occur. This may not be the expected effect. If you access any other page, we will also be a blank page, or a red background (as in the previous helloworl page ). So we can improve local. xml to make sure it is only used for the helloworld page. Modify as follows: Clear the cache. at this time, your other pages should be restored. Then, apply the googbye Aciton Method public function goodbyeAction () {$ this-> loadLayout (); $ this-> renderLayout ();} to load http: // localhost/magento/helloworld/index/goodbye will find the blank page. At this time, we need to add an actionname in the local. xml file. the content is as follows:   Clear cache. at this time, the following two pages will have the same effect. Http: // localhost/magento/helloworld/index/goodbye start to output and getChildHtml is in the standard configuration, the output starts with the root Block (this is the output feature ). We have rewritten the root template = "simple_page.phtml", which will be obtained from the home directory of the current or base topic, for example, app/design/frontend/base/default/template, you can add a template to your own topic or default topic app/design/frontend/default/template app/design/ the frontend/default/custom/template base Directory is the last directory to be searched, if magento cannot be found under other topics, it will return to the base directory. However, you don't want to add this directory as mentioned earlier, because magento updates will overwrite them. The tragedy of adding block-Red content is boring. So we add some content on the page. Change , As shown below I added two embedded blocks to the root account. Magento will allocate it and display a page for customer registration. We need to explicitly call this blockblock in simple_page.html. So we use the getChildHtml method of Block as follows: GetChildHtml ('custom_form_register ');?>Clear the Cache and reload the page. At this time, we can see the registration page on the red background. There is also a Block named top. links. Add the followingLinks GetChildHtml ('Top. link');?>When we reload the page, we can see that the Links is rendered, but the top. links is not rendered. This is because we didn't add it in local. xml. In Layout, getChildHtml can only contain Blocks displayed as a sub-block. This allows Magento to instantiate the blocks it wants. at the same time, we can set different templates for the Block based on the display content. we can add blocks for top. links in local. xml. Clear the cache and you will see the effect of the top. links module.

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.