View.class.php
8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?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();
/**
* 模板主题
* @var theme
* @access protected
*/
protected $theme = '';
/**
* 模板变量赋值
* @access public
* @param mixed $name
* @param mixed $value
*/
public function assign($name,$value=''){
if(is_array($name)) {
$this->tVar = array_merge($this->tVar,$name);
}else {
$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 模板输出内容
* @param string $prefix 模板缓存前缀
* @return mixed
*/
public function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') {
G('viewStartTime');
// 视图开始标签
tag('view_begin',$templateFile);
// 解析并获取模板内容
$content = $this->fetch($templateFile,$content,$prefix);
// 输出模板内容
$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');
if(empty($contentType)) $contentType = C('TMPL_CONTENT_TYPE');
// 网页字符编码
header('Content-Type:'.$contentType.'; charset='.$charset);
header('Cache-control: '.C('HTTP_CACHE_CONTROL')); // 页面缓存控制
header('X-Powered-By:ThinkPHP');
// 输出模板文件
echo $content;
}
/**
* 解析和获取模板内容 用于输出
* @access public
* @param string $templateFile 模板文件名
* @param string $content 模板输出内容
* @param string $prefix 模板缓存前缀
* @return string
*/
public function fetch($templateFile='',$content='',$prefix='') {
if(empty($content)) {
$templateFile = $this->parseTemplate($templateFile);
// 模板文件不存在直接返回
if(!is_file($templateFile))
throw_exception(L('_TEMPLATE_NOT_EXIST_').'['.$templateFile.']');
}
// 页面缓存
ob_start();
ob_implicit_flush(0);
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,'prefix'=>$prefix);
tag('view_parse',$params);
}
// 获取并清空缓存
$content = ob_get_clean();
// 内容过滤标签
tag('view_filter',$content);
// 输出模板文件
return $content;
}
/**
* 自动定位模板文件
* @access protected
* @param string $template 模板文件规则
* @return string
*/
public function parseTemplate($template='') {
$app_name=APP_NAME==basename(dirname($_SERVER['SCRIPT_FILENAME'])) && ''==__APP__?'':APP_NAME.'/';
if(is_file($template)) {
$group = defined('GROUP_NAME')?GROUP_NAME.'/':'';
$theme = C('DEFAULT_THEME');
// 获取当前主题的模版路径
if(1==C('APP_GROUP_MODE')){ // 独立分组模式
define('THEME_PATH', dirname(BASE_LIB_PATH).'/'.$group.basename(TMPL_PATH).'/'.$theme);
define('APP_TMPL_PATH',__ROOT__.'/'.$app_name.C('APP_GROUP_PATH').'/'.$group.basename(TMPL_PATH).'/'.$theme);
}else{
define('THEME_PATH', TMPL_PATH.$group.$theme);
define('APP_TMPL_PATH',__ROOT__.'/'.$app_name.basename(TMPL_PATH).'/'.$group.$theme);
}
return $template;
}
$depr = C('TMPL_FILE_DEPR');
$template = str_replace(':', $depr, $template);
// 获取当前主题名称
$theme = $this->getTemplateTheme();
// 获取当前模版分组
$group = defined('GROUP_NAME')?GROUP_NAME.'/':'';
if(defined('GROUP_NAME') && strpos($template,'@')){ // 跨分组调用模版文件
list($group,$template) = explode('@',$template);
$group .= '/';
}
// 获取当前主题的模版路径
if(1==C('APP_GROUP_MODE')){ // 独立分组模式
define('THEME_PATH', dirname(BASE_LIB_PATH).'/'.$group.basename(TMPL_PATH).'/'.$theme);
define('APP_TMPL_PATH',__ROOT__.'/'.$app_name.C('APP_GROUP_PATH').'/'.$group.basename(TMPL_PATH).'/'.$theme);
}else{
define('THEME_PATH', TMPL_PATH.$group.$theme);
define('APP_TMPL_PATH',__ROOT__.'/'.$app_name.basename(TMPL_PATH).'/'.$group.$theme);
}
// 分析模板文件规则
if('' == $template) {
// 如果模板文件名为空 按照默认规则定位
$template = MODULE_NAME . $depr . ACTION_NAME;
}elseif(false === strpos($template, '/')){
$template = MODULE_NAME . $depr . $template;
}
return THEME_PATH.$template.C('TMPL_TEMPLATE_SUFFIX');
}
/**
* 设置当前输出的模板主题
* @access public
* @param mixed $theme 主题名称
* @return View
*/
public function theme($theme){
$this->theme = $theme;
return $this;
}
/**
* 获取当前的模板主题
* @access private
* @return string
*/
private function getTemplateTheme() {
if($this->theme) { // 指定模板主题
$theme = $this->theme;
}else{
/* 获取模板主题名称 */
$theme = C('DEFAULT_THEME');
if(C('TMPL_DETECT_THEME')) {// 自动侦测模板主题
$t = C('VAR_TEMPLATE');
if (isset($_GET[$t])){
$theme = $_GET[$t];
}elseif(cookie('think_template')){
$theme = cookie('think_template');
}
if(!in_array($theme,explode(',',C('THEME_LIST')))){
$theme = C('DEFAULT_THEME');
}
cookie('think_template',$theme,864000);
}
}
define('THEME_NAME', $theme); // 当前模板主题名称
return $theme?$theme . '/':'';
}
}