PageRenderTime 63ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/ThinkPHP/ThinkPHP/Library/Think/View.class.php

https://github.com/guodont/easycart
PHP | 212 lines | 102 code | 12 blank | 98 comment | 22 complexity | f38f13ef406b3fd34011b3ce0c16a787 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think;
  12. /**
  13. * ThinkPHP 视图类
  14. */
  15. class View {
  16. /**
  17. * 模板输出变量
  18. * @var tVar
  19. * @access protected
  20. */
  21. protected $tVar = array();
  22. /**
  23. * 模板主题
  24. * @var theme
  25. * @access protected
  26. */
  27. protected $theme = '';
  28. /**
  29. * 模板变量赋值
  30. * @access public
  31. * @param mixed $name
  32. * @param mixed $value
  33. */
  34. public function assign($name,$value=''){
  35. if(is_array($name)) {
  36. $this->tVar = array_merge($this->tVar,$name);
  37. }else {
  38. $this->tVar[$name] = $value;
  39. }
  40. }
  41. /**
  42. * 取得模板变量的值
  43. * @access public
  44. * @param string $name
  45. * @return mixed
  46. */
  47. public function get($name=''){
  48. if('' === $name) {
  49. return $this->tVar;
  50. }
  51. return isset($this->tVar[$name])?$this->tVar[$name]:false;
  52. }
  53. /**
  54. * 加载模板和页面输出 可以返回输出内容
  55. * @access public
  56. * @param string $templateFile 模板文件名
  57. * @param string $charset 模板输出字符集
  58. * @param string $contentType 输出类型
  59. * @param string $content 模板输出内容
  60. * @param string $prefix 模板缓存前缀
  61. * @return mixed
  62. */
  63. public function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') {
  64. G('viewStartTime');
  65. // 视图开始标签
  66. Hook::listen('view_begin',$templateFile);
  67. // 解析并获取模板内容
  68. $content = $this->fetch($templateFile,$content,$prefix);
  69. // 输出模板内容
  70. $this->render($content,$charset,$contentType);
  71. // 视图结束标签
  72. Hook::listen('view_end');
  73. }
  74. /**
  75. * 输出内容文本可以包括Html
  76. * @access private
  77. * @param string $content 输出内容
  78. * @param string $charset 模板输出字符集
  79. * @param string $contentType 输出类型
  80. * @return mixed
  81. */
  82. private function render($content,$charset='',$contentType=''){
  83. if(empty($charset)) $charset = C('DEFAULT_CHARSET');
  84. if(empty($contentType)) $contentType = C('TMPL_CONTENT_TYPE');
  85. // 网页字符编码
  86. header('Content-Type:'.$contentType.'; charset='.$charset);
  87. header('Cache-control: '.C('HTTP_CACHE_CONTROL')); // 页面缓存控制
  88. header('X-Powered-By:ThinkPHP');
  89. // 输出模板文件
  90. echo $content;
  91. }
  92. /**
  93. * 解析和获取模板内容 用于输出
  94. * @access public
  95. * @param string $templateFile 模板文件名
  96. * @param string $content 模板输出内容
  97. * @param string $prefix 模板缓存前缀
  98. * @return string
  99. */
  100. public function fetch($templateFile='',$content='',$prefix='') {
  101. if(empty($content)) {
  102. $templateFile = $this->parseTemplate($templateFile);
  103. // 模板文件不存在直接返回
  104. if(!is_file($templateFile)) E(L('_TEMPLATE_NOT_EXIST_').':'.$templateFile);
  105. }
  106. // 页面缓存
  107. ob_start();
  108. ob_implicit_flush(0);
  109. if('php' == strtolower(C('TMPL_ENGINE_TYPE'))) { // 使用PHP原生模板
  110. // 模板阵列变量分解成为独立变量
  111. extract($this->tVar, EXTR_OVERWRITE);
  112. // 直接载入PHP模板
  113. empty($content)?include $templateFile:eval('?>'.$content);
  114. }else{
  115. // 视图解析标签
  116. $params = array('var'=>$this->tVar,'file'=>$templateFile,'content'=>$content,'prefix'=>$prefix);
  117. Hook::listen('view_parse',$params);
  118. }
  119. // 获取并清空缓存
  120. $content = ob_get_clean();
  121. // 内容过滤标签
  122. Hook::listen('view_filter',$content);
  123. // 输出模板文件
  124. return $content;
  125. }
  126. /**
  127. * 自动定位模板文件
  128. * @access protected
  129. * @param string $template 模板文件规则
  130. * @return string
  131. */
  132. public function parseTemplate($template='') {
  133. if(is_file($template)) {
  134. return $template;
  135. }
  136. $depr = C('TMPL_FILE_DEPR');
  137. $template = str_replace(':', $depr, $template);
  138. // 获取当前主题名称
  139. $theme = $this->getTemplateTheme();
  140. // 获取当前模块
  141. $module = MODULE_NAME;
  142. if(strpos($template,'@')){ // 跨模块调用模版文件
  143. list($module,$template) = explode('@',$template);
  144. }
  145. // 获取当前主题的模版路径
  146. if(!defined('THEME_PATH')){
  147. define('THEME_PATH', C('VIEW_PATH')? C('VIEW_PATH').$theme : APP_PATH.$module.'/'.C('DEFAULT_V_LAYER').'/'.$theme);
  148. }
  149. // 分析模板文件规则
  150. if('' == $template) {
  151. // 如果模板文件名为空 按照默认规则定位
  152. $template = CONTROLLER_NAME . $depr . ACTION_NAME;
  153. }elseif(false === strpos($template, $depr)){
  154. $template = CONTROLLER_NAME . $depr . $template;
  155. }
  156. $file = THEME_PATH.$template.C('TMPL_TEMPLATE_SUFFIX');
  157. if(C('TMPL_LOAD_DEFAULTTHEME') && THEME_NAME != C('DEFAULT_THEME') && !is_file($file)){
  158. // 找不到当前主题模板的时候定位默认主题中的模板
  159. $file = dirname(THEME_PATH).'/'.C('DEFAULT_THEME').'/'.$template.C('TMPL_TEMPLATE_SUFFIX');
  160. }
  161. return $file;
  162. }
  163. /**
  164. * 设置当前输出的模板主题
  165. * @access public
  166. * @param mixed $theme 主题名称
  167. * @return View
  168. */
  169. public function theme($theme){
  170. $this->theme = $theme;
  171. return $this;
  172. }
  173. /**
  174. * 获取当前的模板主题
  175. * @access private
  176. * @return string
  177. */
  178. private function getTemplateTheme() {
  179. if($this->theme) { // 指定模板主题
  180. $theme = $this->theme;
  181. }else{
  182. /* 获取模板主题名称 */
  183. $theme = C('DEFAULT_THEME');
  184. if(C('TMPL_DETECT_THEME')) {// 自动侦测模板主题
  185. $t = C('VAR_TEMPLATE');
  186. if (isset($_GET[$t])){
  187. $theme = $_GET[$t];
  188. }elseif(cookie('think_template')){
  189. $theme = cookie('think_template');
  190. }
  191. if(!in_array($theme,explode(',',C('THEME_LIST')))){
  192. $theme = C('DEFAULT_THEME');
  193. }
  194. cookie('think_template',$theme,864000);
  195. }
  196. }
  197. defined('THEME_NAME') || define('THEME_NAME', $theme); // 当前模板主题名称
  198. return $theme?$theme . '/':'';
  199. }
  200. }