magento的layout xml是如何工作的

日期:2016-09-08 阅读:1311

dmandwp系统 - wordpress系统和DM系统区块建站>>

A layout is comprised of default layout and layout updates that are made up of easy-to-learn XML tags. With these layout commands, you can modify/assign content block-structural block relationships and also control store-front functionalities such as loading and unloading of block-specific JavaScripts to a page.

layout文件是xml文件, 可以用来布局和控制js是否加载等。

XML tags 介绍:


Each Magento module has it is own layout file, for instance ”catalog.xml” is a layout file for the catalog module” 
and “customer.xml” is for the customer module. 每个模块都有一个布局的xml文件。 

Each file is further separated into handles. xml文件具体由handles组成。

The handles that are available are pre-defined in the Magento core and by any active extensions in your store. 
For the most part, each handle corresponds to a type of page within your Magento store, 
such as <catalog_category_default> and <catalog_product_view>
but there are some handles that apply to all pages, such as the <default> handle, 
and some that apply within pages based on specific statuses, such as <customer_logged_in> and <customer_logged_out>.


A handle can occur in multiple layout files and blocks can be assigned to that handle or overridden by each layout file. For example, most layout files may contain the <default> handle. When parsing the layout files, Magento first grabs the layout updates assigned in the <default> handle in each of the layout files, reading them in the order as assigned in app/etc/modules/Mage_All.xml. It then parses the page-specific layout handles, again looking in all of the layout files for that handle, and finalizes the building of a store page.


Magento determines the behavior and visual representation of each building block of a page via the <block> tag. We have already mentioned the two types of blocks Magento employs - structural blocks and content blocks. The best way to distinguish between the two is by examining the behavior assigned to each via the tag attributes. A structural block usually contains the attribute ”as,” through which the application is able to communicate with the designated area (using the getChildHtml method) in a template. You will notice many occurrences of this ”as” attribute in the default layout, because by nature the default layout is one that builds the groundwork upon which the page-specific layouts can begin adding onto. For instance, in the default layout, there are structural blocks such as ”left,” ”right,” ”content” and ”footer” being introduced. Although these blocks can exist in normal layout updates, why not first set up the reoccurring structural blocks in the default layout first, and then start adding content on a per-page basisThe available attributes for <block> are:

type – This is the identifier of the module class that defines the functionality of the block. This attribute must not be modified.
name – This is the name by which other blocks can make reference to the block in which this attribute is assigned 

before (and) after – These are two ways to position a content block within a structural block. before=“-” and after=“-” are commands used to position the block accordingly at the very top or very bottom of a structural block.
template - This attribute determines the template that will represent the functionality of the block in which this attribute is assigned. For instance, if this attributes is assigned ‘catalog/category/view.phtml’, the application will load the ‘app/design/frontend/template/catalog/category/view.phtml template file.

action – <action> is used to control store-front functionalities such as loading or unloading of a JavaScript. A full list of action methods will soon become available, but in the mean time the best way to learn about the different action methods is by playing around with them in the currently available layout updates.
as – This is the name by which a template calls the block in which this attribute is assigned. When you see the getChildHtml(‘block_name’) PHP method called from a template, it is referring to the block whose attribute ”as” is assigned the name ‘block_name’. (i.e. The method <?=$this->getChildHtml(‘header’)?> in the a skeleton template correlates to <block as=“header”> ).  - as 表示分配给某个区块,可以用

<reference> is used to make reference to another block. By making a reference to another block, the updates inside <reference> will apply to the <block> to which it correlates (如下图).
To make the reference, you must target the reference to a block by using the ”name” attribute. This attribute targets the <block> tag’s ”name” attribute. Therefore, if you were to make a reference by <reference name=“right”>, you are targeting the block named <block name=“right“>.