>  기사  >  백엔드 개발  >  Magento 开发笔记3_PHP教程

Magento 开发笔记3_PHP教程

WBOY
WBOY원래의
2016-07-14 10:07:14736검색

我们在这个部分关注一下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() ?>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.