View.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <[email protected]>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. class View
  13. {
  14. // 视图实例
  15. protected static $instance;
  16. // 模板引擎实例
  17. public $engine;
  18. // 模板变量
  19. protected $data = [];
  20. // 用于静态赋值的模板变量
  21. protected static $var = [];
  22. // 视图输出替换
  23. protected $replace = [];
  24. /**
  25. * 构造函数
  26. * @access public
  27. * @param array $engine 模板引擎参数
  28. * @param array $replace 字符串替换参数
  29. */
  30. public function __construct($engine = [], $replace = [])
  31. {
  32. // 初始化模板引擎
  33. $this->engine($engine);
  34. // 基础替换字符串
  35. $request = Request::instance();
  36. $base = $request->root();
  37. $root = strpos($base, '.') ? ltrim(dirname($base), DS) : $base;
  38. if ('' != $root) {
  39. $root = '/' . ltrim($root, '/');
  40. }
  41. // 如果 new_version 为 1 或者 new_version 不存在或者为null,则使用新版模板
  42. if($GLOBALS['config']['site']['new_version'] == 1 || !isset($GLOBALS['config']['site']['new_version']) || empty($GLOBALS['config']['site']['new_version'])){
  43. $root . $static_path = '/static_new/';
  44. }else{
  45. $root . $static_path = '/static/';
  46. }
  47. $baseReplace = [
  48. '__ROOT__' => $root,
  49. 'MAC_BASE_PATH' => $root,
  50. '__URL__' => $base . '/' . $request->module() . '/' . Loader::parseName($request->controller()),
  51. '__STATIC__' => $root . $static_path,
  52. '__CSS__' => $root . $static_path . '/css',
  53. '__JS__' => $root . $static_path .'/js',
  54. ];
  55. $this->assign('MAC_BASE_PATH', $this->mac_base_path());
  56. $this->replace = array_merge($baseReplace, (array) $replace);
  57. }
  58. public function mac_base_path()
  59. {
  60. return $GLOBALS['rootpath'];
  61. }
  62. /**
  63. * 初始化视图
  64. * @access public
  65. * @param array $engine 模板引擎参数
  66. * @param array $replace 字符串替换参数
  67. * @return object
  68. */
  69. public static function instance($engine = [], $replace = [])
  70. {
  71. if (is_null(self::$instance)) {
  72. self::$instance = new self($engine, $replace);
  73. }
  74. return self::$instance;
  75. }
  76. /**
  77. * 模板变量静态赋值
  78. * @access public
  79. * @param mixed $name 变量名
  80. * @param mixed $value 变量值
  81. * @return void
  82. */
  83. public static function share($name, $value = '')
  84. {
  85. if (is_array($name)) {
  86. self::$var = array_merge(self::$var, $name);
  87. } else {
  88. self::$var[$name] = $value;
  89. }
  90. }
  91. /**
  92. * 模板变量赋值
  93. * @access public
  94. * @param mixed $name 变量名
  95. * @param mixed $value 变量值
  96. * @return $this
  97. */
  98. public function assign($name, $value = '')
  99. {
  100. if (is_array($name)) {
  101. $this->data = array_merge($this->data, $name);
  102. } else {
  103. $this->data[$name] = $value;
  104. }
  105. return $this;
  106. }
  107. /**
  108. * 设置当前模板解析的引擎
  109. * @access public
  110. * @param array|string $options 引擎参数
  111. * @return $this
  112. */
  113. public function engine($options = [])
  114. {
  115. if (is_string($options)) {
  116. $type = $options;
  117. $options = [];
  118. } else {
  119. $type = !empty($options['type']) ? $options['type'] : 'Think';
  120. }
  121. $class = false !== strpos($type, '\\') ? $type : '\\think\\view\\driver\\' . ucfirst($type);
  122. if (isset($options['type'])) {
  123. unset($options['type']);
  124. }
  125. $this->engine = new $class($options);
  126. return $this;
  127. }
  128. /**
  129. * 配置模板引擎
  130. * @access private
  131. * @param string|array $name 参数名
  132. * @param mixed $value 参数值
  133. * @return $this
  134. */
  135. public function config($name, $value = null)
  136. {
  137. $this->engine->config($name, $value);
  138. return $this;
  139. }
  140. /**
  141. * 解析和获取模板内容 用于输出
  142. * @param string $template 模板文件名或者内容
  143. * @param array $vars 模板输出变量
  144. * @param array $replace 替换内容
  145. * @param array $config 模板参数
  146. * @param bool $renderContent 是否渲染内容
  147. * @return string
  148. * @throws Exception
  149. */
  150. public function fetch($template = '', $vars = [], $replace = [], $config = [], $renderContent = false)
  151. {
  152. // 模板变量
  153. $vars = array_merge(self::$var, $this->data, $vars);
  154. // 页面缓存
  155. ob_start();
  156. ob_implicit_flush(0);
  157. // 渲染输出
  158. try {
  159. $method = $renderContent ? 'display' : 'fetch';
  160. // 允许用户自定义模板的字符串替换
  161. $replace = array_merge($this->replace, $replace, (array) $this->engine->config('tpl_replace_string'));
  162. $this->engine->config('tpl_replace_string', $replace);
  163. $this->engine->$method($template, $vars, $config);
  164. } catch (\Exception $e) {
  165. ob_end_clean();
  166. throw $e;
  167. }
  168. // 获取并清空缓存
  169. $content = ob_get_clean();
  170. // 内容过滤标签
  171. Hook::listen('view_filter', $content);
  172. return $content;
  173. }
  174. /**
  175. * 视图内容替换
  176. * @access public
  177. * @param string|array $content 被替换内容(支持批量替换)
  178. * @param string $replace 替换内容
  179. * @return $this
  180. */
  181. public function replace($content, $replace = '')
  182. {
  183. if (is_array($content)) {
  184. $this->replace = array_merge($this->replace, $content);
  185. } else {
  186. $this->replace[$content] = $replace;
  187. }
  188. return $this;
  189. }
  190. /**
  191. * 渲染内容输出
  192. * @access public
  193. * @param string $content 内容
  194. * @param array $vars 模板输出变量
  195. * @param array $replace 替换内容
  196. * @param array $config 模板参数
  197. * @return mixed
  198. */
  199. public function display($content, $vars = [], $replace = [], $config = [])
  200. {
  201. return $this->fetch($content, $vars, $replace, $config, true);
  202. }
  203. /**
  204. * 模板变量赋值
  205. * @access public
  206. * @param string $name 变量名
  207. * @param mixed $value 变量值
  208. */
  209. public function __set($name, $value)
  210. {
  211. $this->data[$name] = $value;
  212. }
  213. /**
  214. * 取得模板显示变量的值
  215. * @access protected
  216. * @param string $name 模板变量
  217. * @return mixed
  218. */
  219. public function __get($name)
  220. {
  221. return $this->data[$name];
  222. }
  223. /**
  224. * 检测模板变量是否设置
  225. * @access public
  226. * @param string $name 模板变量名
  227. * @return bool
  228. */
  229. public function __isset($name)
  230. {
  231. return isset($this->data[$name]);
  232. }
  233. }