View.class.php 8.1 KB

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