View.php 6.9 KB

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