View.php 6.8 KB

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