View.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. if($GLOBALS['config']['site']['new_version'] == 1){
  42. $static_path = '/static_new/';
  43. }else{
  44. $static_path = '/static/';
  45. }
  46. $baseReplace = [
  47. '__ROOT__' => $root,
  48. 'MAC_BASE_PATH' => $root,
  49. '__URL__' => $base . '/' . $request->module() . '/' . Loader::parseName($request->controller()),
  50. '__STATIC__' => $root . $static_path,
  51. '__CSS__' => $root . $static_path . '/css',
  52. '__JS__' => $root . $static_path .'/js',
  53. ];
  54. $this->assign('MAC_BASE_PATH', $this->mac_base_path());
  55. $this->replace = array_merge($baseReplace, (array) $replace);
  56. }
  57. public function mac_base_path()
  58. {
  59. return $GLOBALS['rootpath'];
  60. }
  61. /**
  62. * 初始化视图
  63. * @access public
  64. * @param array $engine 模板引擎参数
  65. * @param array $replace 字符串替换参数
  66. * @return object
  67. */
  68. public static function instance($engine = [], $replace = [])
  69. {
  70. if (is_null(self::$instance)) {
  71. self::$instance = new self($engine, $replace);
  72. }
  73. return self::$instance;
  74. }
  75. /**
  76. * 模板变量静态赋值
  77. * @access public
  78. * @param mixed $name 变量名
  79. * @param mixed $value 变量值
  80. * @return void
  81. */
  82. public static function share($name, $value = '')
  83. {
  84. if (is_array($name)) {
  85. self::$var = array_merge(self::$var, $name);
  86. } else {
  87. self::$var[$name] = $value;
  88. }
  89. }
  90. /**
  91. * 模板变量赋值
  92. * @access public
  93. * @param mixed $name 变量名
  94. * @param mixed $value 变量值
  95. * @return $this
  96. */
  97. public function assign($name, $value = '')
  98. {
  99. if (is_array($name)) {
  100. $this->data = array_merge($this->data, $name);
  101. } else {
  102. $this->data[$name] = $value;
  103. }
  104. return $this;
  105. }
  106. /**
  107. * 设置当前模板解析的引擎
  108. * @access public
  109. * @param array|string $options 引擎参数
  110. * @return $this
  111. */
  112. public function engine($options = [])
  113. {
  114. if (is_string($options)) {
  115. $type = $options;
  116. $options = [];
  117. } else {
  118. $type = !empty($options['type']) ? $options['type'] : 'Think';
  119. }
  120. $class = false !== strpos($type, '\\') ? $type : '\\think\\view\\driver\\' . ucfirst($type);
  121. if (isset($options['type'])) {
  122. unset($options['type']);
  123. }
  124. $this->engine = new $class($options);
  125. return $this;
  126. }
  127. /**
  128. * 配置模板引擎
  129. * @access private
  130. * @param string|array $name 参数名
  131. * @param mixed $value 参数值
  132. * @return $this
  133. */
  134. public function config($name, $value = null)
  135. {
  136. $this->engine->config($name, $value);
  137. return $this;
  138. }
  139. /**
  140. * 解析和获取模板内容 用于输出
  141. * @param string $template 模板文件名或者内容
  142. * @param array $vars 模板输出变量
  143. * @param array $replace 替换内容
  144. * @param array $config 模板参数
  145. * @param bool $renderContent 是否渲染内容
  146. * @return string
  147. * @throws Exception
  148. */
  149. public function fetch($template = '', $vars = [], $replace = [], $config = [], $renderContent = false)
  150. {
  151. // 模板变量
  152. $vars = array_merge(self::$var, $this->data, $vars);
  153. // 页面缓存
  154. ob_start();
  155. ob_implicit_flush(0);
  156. // 渲染输出
  157. try {
  158. $method = $renderContent ? 'display' : 'fetch';
  159. // 允许用户自定义模板的字符串替换
  160. $replace = array_merge($this->replace, $replace, (array) $this->engine->config('tpl_replace_string'));
  161. $this->engine->config('tpl_replace_string', $replace);
  162. $this->engine->$method($template, $vars, $config);
  163. } catch (\Exception $e) {
  164. ob_end_clean();
  165. throw $e;
  166. }
  167. // 获取并清空缓存
  168. $content = ob_get_clean();
  169. // 内容过滤标签
  170. Hook::listen('view_filter', $content);
  171. return $content;
  172. }
  173. /**
  174. * 视图内容替换
  175. * @access public
  176. * @param string|array $content 被替换内容(支持批量替换)
  177. * @param string $replace 替换内容
  178. * @return $this
  179. */
  180. public function replace($content, $replace = '')
  181. {
  182. if (is_array($content)) {
  183. $this->replace = array_merge($this->replace, $content);
  184. } else {
  185. $this->replace[$content] = $replace;
  186. }
  187. return $this;
  188. }
  189. /**
  190. * 渲染内容输出
  191. * @access public
  192. * @param string $content 内容
  193. * @param array $vars 模板输出变量
  194. * @param array $replace 替换内容
  195. * @param array $config 模板参数
  196. * @return mixed
  197. */
  198. public function display($content, $vars = [], $replace = [], $config = [])
  199. {
  200. return $this->fetch($content, $vars, $replace, $config, true);
  201. }
  202. /**
  203. * 模板变量赋值
  204. * @access public
  205. * @param string $name 变量名
  206. * @param mixed $value 变量值
  207. */
  208. public function __set($name, $value)
  209. {
  210. $this->data[$name] = $value;
  211. }
  212. /**
  213. * 取得模板显示变量的值
  214. * @access protected
  215. * @param string $name 模板变量
  216. * @return mixed
  217. */
  218. public function __get($name)
  219. {
  220. return $this->data[$name];
  221. }
  222. /**
  223. * 检测模板变量是否设置
  224. * @access public
  225. * @param string $name 模板变量名
  226. * @return bool
  227. */
  228. public function __isset($name)
  229. {
  230. return isset($this->data[$name]);
  231. }
  232. }