Heim  >  Artikel  >  Backend-Entwicklung  >  Magento 开发笔记3_PHP教程

Magento 开发笔记3_PHP教程

WBOY
WBOYOriginal
2016-07-14 10:07:14737Durchsuche

我们在这个部分关注一下View里Layouts和Blocks。

 
跟其他主流PHPMVC架构不一样,magento 的ActionController不会把数据对象传给view,也不会设置View对象里的属性。View是通过系统模块去获取它所需要的信息。
 
这个设计的结果是View被分为Blocks 和Templates。Blocks是PHP对象,Templates是PHP代码和HTML的混合(也可以认为是PHP作为了模版语言)。每个Block绑定到一个Template文件。在一个Phtml文件里,PHP的关键字$this会包含了对Temeplate对应Block的引用。
 
下面是一个快速的例子。查看模版文件app/design/frontend/base/default/template/catalog/product/list.phtml
 
会看到如下的代码
 
getLoadedProductCollection() ?>    
 
count()): ?> 
 
    
 
__("There are no products matching the selection.")?>    
 
 
 
 
 
其中的getLoadedProudctController可以在对应的block文件找到
 
app/code/core/Mage/Catalog/Block/Product/List.php
 
public functiongetLoadedProductCollection()
 
{
 
                     return$this->_getProductCollection();
 
}
 
其中的_getProductCollection会实例化models,并取到数据给对应template。
 
内嵌Block
Blocks/Templates真正的强大的地方是getChildHtml方法。这可以让我们包含次一级的Block/Template在主的Block/Template里面(xml的格式)
 
Blocks调用Blocks是会组成我们整个HTMLlayout。看一个例子
 
App/design/frotend/base/default/template/page/1column.phtml
 
-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> getChildHtml('head') ?>     getChildHtml('content') ?>    getChildHtml('before_body_end') ?>    
 
getAbsoluteFooter() ?>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn