【thinkphp3.x】ThinkPHP/Lib/Core/View.class.php文件分析
2012-10-01 20:13
681 查看
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ] // +---------------------------------------------------------------------- // | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- /** * ThinkPHP 视图类 * @category Think * @package Think * @subpackage Core * @author liu21st <liu21st@gmail.com> */ class View { /** * 模板输出变量 * @var tVar * @access protected */ protected $tVar = array(); /** * 模板变量赋值 * @access public * @param mixed $name * @param mixed $value */ public function assign($name,$value=''){ if(is_array($name)) {//$name为数组的情况 $this->tVar = array_merge($this->tVar,$name); }else {//$name为非数组的情况 $this->tVar[$name] = $value; } } /** * 取得模板变量的值 * @access public * @param string $name * @return mixed */ public function get($name=''){ if('' === $name) {//为空,则获取所有模版变量的值 return $this->tVar; } return isset($this->tVar[$name])?$this->tVar[$name]:false; } /** * 加载模板和页面输出 可以返回输出内容 * @access public * @param string $templateFile 模板文件名 * @param string $charset 模板输出字符集 * @param string $contentType 输出类型 * @param string $content 模板输出内容 * @return mixed */ public function display($templateFile='',$charset='',$contentType='',$content='') { G('viewStartTime'); // 视图开始标签 tag('view_begin',$templateFile); // 解析并获取模板内容,注意:返回解析后的模板内容 $content = $this->fetch($templateFile,$content); // 输出模板内容 $this->render($content,$charset,$contentType); // 视图结束标签 tag('view_end'); } /** * 输出内容文本可以包括Html * @access private * @param string $content 输出内容 * @param string $charset 模板输出字符集 * @param string $contentType 输出类型 * @return mixed */ private function render($content,$charset='',$contentType=''){ if(empty($charset)) $charset = C('DEFAULT_CHARSET');//默认输出编码:utf-8 if(empty($contentType)) $contentType = C('TMPL_CONTENT_TYPE'); //默认模版的输出类型:text/html // 网页字符编码 header('Content-Type:'.$contentType.'; charset='.$charset); header('Cache-control: '.C('HTTP_CACHE_CONTROL')); // 页面缓存控制,默认值为private header('X-Powered-By:ThinkPHP'); // 输出模板文件内容 echo $content; } /** * 解析和获取模板内容 用于输出 * @access public * @param string $templateFile 模板文件名 * @param string $content 模板输出内容 * @return string */ public function fetch($templateFile='',$content='') { if(empty($content)) {//内容为空的情况 // 模板文件解析标签 tag('view_template',$templateFile);//自动定位模板文件,$templateFile传递给LocationTemplateBehavior.class.php行为类中的run方法 // 模板文件不存在直接返回 //此时$templateFile的值:"./xmall/Tpl/Index/index.html",而不是"index" if(!is_file($templateFile)) return NULL; } // 页面缓存 ob_start();//开启缓冲区 ob_implicit_flush(0); //C('TMPL_ENGINE_TYPE'):默认值为空 if('php' == strtolower(C('TMPL_ENGINE_TYPE'))) { // 使用PHP原生模板,模版引擎的类型,默认不执行 // 模板阵列变量分解成为独立变量 extract($this->tVar, EXTR_OVERWRITE); // 直接载入PHP模板 empty($content)?include $templateFile:eval('?>'.$content); }else{//默认执行 // 视图解析标签 $params = array('var'=>$this->tVar,'file'=>$templateFile,'content'=>$content); //执行下面这行代码后,模版引擎的类型是think tag('view_parse',$params);//模版解析,$params数组传递给ParseTemplateBehavior.class.php行为类中的run方法 } // 获取并清空缓存 $content = ob_get_clean();//这里获取的是编译后的缓存php文件的内容 // 内容过滤标签 tag('view_filter',$content);//主要用来模版文件中常量的替换,如:__PUBLIC__、__URL__、__ROOT__等的替换 // 输出模板文件 return $content; } }
相关文章推荐
- 【thinkphp3.x】ThinkPHP/Lib/Core/App.class.php文件分析
- 【thinkphp3.x】ThinkPHP/Lib/Core/Model.class.php文件分析
- 【thinkphp3.x】ThinkPHP/Lib/Core/Think.class.php文件分析
- 【thinkphp3.x】ThinkPHP/Lib/Core/Dispatcher.class.php文件分析
- 【thinkphp3.x】ThinkPHP/Lib/Behavior/ParseTemplateBehavior.class.php文件分析
- 【thinkphp3.x】ThinkPHP/Lib/Behavior/ContentReplaceBehavior.class.php文件分析
- 【thinkphp3.x】Model.class.php文件分析
- 【discuzX2】/source/class/class_core.php文件中核心高效缓存类discuz_memory分析
- 【discuzX2】/source/class/class_core.php文件中核心基础类库中discuz_core类分析
- discuz 3.x 核心文件class_core.php解析
- 【thinkphp3.x】ThinkPHP.php文件分析
- 【thinkphp3.x】ThinkPHP/Common/runtime.php文件分析
- 【discuzX2】/source/class/class_core.php文件中核心基础类库中discuz_core类分析
- thinkphp文件处理类Dir.class.php的用法分析
- 【discuzX2】/source/class/class_core.php文件中数据库操作类DB及db_mysql分析
- 【discuzX2】/source/class/class_core.php文件中session会话类discuz_session分析
- 【discuzX2】/source/class/class_core.php文件中核心高效缓存类discuz_memory分析
- DISCUZX2.0 核心文件 class_core.php 分析之 – discuz_process
- 【discuzX2】/source/class/class_core.php文件中数据库操作类DB及db_mysql分析
- 【thinkphp3.x】ThinkPHP/Common/common.php文件中某些函数的分析