View.php 5.7 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 <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. ob_implicit_flush(0);
  164. // 渲染输出
  165. try {
  166. $method = $renderContent ? 'display' : 'fetch';
  167. $this->engine->$method($template, $vars, $config);
  168. } catch (\Exception $e) {
  169. ob_end_clean();
  170. throw $e;
  171. }
  172. // 获取并清空缓存
  173. $content = ob_get_clean();
  174. if ($this->filter) {
  175. $content = call_user_func_array($this->filter, [$content]);
  176. }
  177. return $content;
  178. }
  179. /**
  180. * 渲染内容输出
  181. * @access public
  182. * @param string $content 内容
  183. * @param array $vars 模板输出变量
  184. * @param array $config 模板参数
  185. * @return mixed
  186. */
  187. public function display($content, $vars = [], $config = [])
  188. {
  189. return $this->fetch($content, $vars, $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. }