No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

View.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. * 模板引擎实例
  16. * @var object
  17. */
  18. public $engine;
  19. /**
  20. * 模板变量
  21. * @var array
  22. */
  23. protected $data = [];
  24. /**
  25. * 内容过滤
  26. * @var mixed
  27. */
  28. protected $filter;
  29. /**
  30. * 全局模板变量
  31. * @var array
  32. */
  33. protected static $var = [];
  34. /**
  35. * 初始化
  36. * @access public
  37. * @param mixed $engine 模板引擎参数
  38. * @return $this
  39. */
  40. public function init($engine = [])
  41. {
  42. // 初始化模板引擎
  43. $this->engine($engine);
  44. return $this;
  45. }
  46. public static function __make(Config $config)
  47. {
  48. return (new static())->init($config->pull('template'));
  49. }
  50. /**
  51. * 模板变量静态赋值
  52. * @access public
  53. * @param mixed $name 变量名
  54. * @param mixed $value 变量值
  55. * @return $this
  56. */
  57. public function share($name, $value = '')
  58. {
  59. if (is_array($name)) {
  60. self::$var = array_merge(self::$var, $name);
  61. } else {
  62. self::$var[$name] = $value;
  63. }
  64. return $this;
  65. }
  66. /**
  67. * 清理模板变量
  68. * @access public
  69. * @return void
  70. */
  71. public function clear()
  72. {
  73. self::$var = [];
  74. $this->data = [];
  75. }
  76. /**
  77. * 模板变量赋值
  78. * @access public
  79. * @param mixed $name 变量名
  80. * @param mixed $value 变量值
  81. * @return $this
  82. */
  83. public function assign($name, $value = '')
  84. {
  85. if (is_array($name)) {
  86. $this->data = array_merge($this->data, $name);
  87. } else {
  88. $this->data[$name] = $value;
  89. }
  90. return $this;
  91. }
  92. /**
  93. * 设置当前模板解析的引擎
  94. * @access public
  95. * @param array|string $options 引擎参数
  96. * @return $this
  97. */
  98. public function engine($options = [])
  99. {
  100. if (is_string($options)) {
  101. $type = $options;
  102. $options = [];
  103. } else {
  104. $type = !empty($options['type']) ? $options['type'] : 'Think';
  105. }
  106. if (isset($options['type'])) {
  107. unset($options['type']);
  108. }
  109. $this->engine = Loader::factory($type, '\\think\\view\\driver\\', $options);
  110. return $this;
  111. }
  112. /**
  113. * 配置模板引擎
  114. * @access public
  115. * @param string|array $name 参数名
  116. * @param mixed $value 参数值
  117. * @return $this
  118. */
  119. public function config($name, $value = null)
  120. {
  121. $this->engine->config($name, $value);
  122. return $this;
  123. }
  124. /**
  125. * 检查模板是否存在
  126. * @access public
  127. * @param string|array $name 参数名
  128. * @return bool
  129. */
  130. public function exists($name)
  131. {
  132. return $this->engine->exists($name);
  133. }
  134. /**
  135. * 视图过滤
  136. * @access public
  137. * @param Callable $filter 过滤方法或闭包
  138. * @return $this
  139. */
  140. public function filter($filter)
  141. {
  142. if ($filter) {
  143. $this->filter = $filter;
  144. }
  145. return $this;
  146. }
  147. /**
  148. * 解析和获取模板内容 用于输出
  149. * @access public
  150. * @param string $template 模板文件名或者内容
  151. * @param array $vars 模板输出变量
  152. * @param array $config 模板参数
  153. * @param bool $renderContent 是否渲染内容
  154. * @return string
  155. * @throws \Exception
  156. */
  157. public function fetch($template = '', $vars = [], $config = [], $renderContent = false)
  158. {
  159. // 模板变量
  160. $vars = array_merge(self::$var, $this->data, $vars);
  161. // 页面缓存
  162. ob_start();
  163. if (version_compare(PHP_VERSION, '8.0', '>=')) {
  164. ob_implicit_flush(false);
  165. } else {
  166. ob_implicit_flush(0);
  167. }
  168. // 渲染输出
  169. try {
  170. $method = $renderContent ? 'display' : 'fetch';
  171. $this->engine->$method($template, $vars, $config);
  172. } catch (\Exception $e) {
  173. ob_end_clean();
  174. throw $e;
  175. }
  176. // 获取并清空缓存
  177. $content = ob_get_clean();
  178. if ($this->filter) {
  179. $content = call_user_func_array($this->filter, [$content]);
  180. }
  181. return $content;
  182. }
  183. /**
  184. * 渲染内容输出
  185. * @access public
  186. * @param string $content 内容
  187. * @param array $vars 模板输出变量
  188. * @param array $config 模板参数
  189. * @return mixed
  190. */
  191. public function display($content, $vars = [], $config = [])
  192. {
  193. return $this->fetch($content, $vars, $config, true);
  194. }
  195. /**
  196. * 模板变量赋值
  197. * @access public
  198. * @param string $name 变量名
  199. * @param mixed $value 变量值
  200. */
  201. public function __set($name, $value)
  202. {
  203. $this->data[$name] = $value;
  204. }
  205. /**
  206. * 取得模板显示变量的值
  207. * @access protected
  208. * @param string $name 模板变量
  209. * @return mixed
  210. */
  211. public function __get($name)
  212. {
  213. return $this->data[$name];
  214. }
  215. /**
  216. * 检测模板变量是否设置
  217. * @access public
  218. * @param string $name 模板变量名
  219. * @return bool
  220. */
  221. public function __isset($name)
  222. {
  223. return isset($this->data[$name]);
  224. }
  225. }