View.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2021 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. declare (strict_types = 1);
  12. namespace think;
  13. use think\helper\Arr;
  14. /**
  15. * 视图类
  16. * @package think
  17. */
  18. class View extends Manager
  19. {
  20. protected $namespace = '\\think\\view\\driver\\';
  21. /**
  22. * 模板变量
  23. * @var array
  24. */
  25. protected $data = [];
  26. /**
  27. * 内容过滤
  28. * @var mixed
  29. */
  30. protected $filter;
  31. /**
  32. * 获取模板引擎
  33. * @access public
  34. * @param string $type 模板引擎类型
  35. * @return $this
  36. */
  37. public function engine(string $type = null)
  38. {
  39. return $this->driver($type);
  40. }
  41. /**
  42. * 模板变量赋值
  43. * @access public
  44. * @param string|array $name 模板变量
  45. * @param mixed $value 变量值
  46. * @return $this
  47. */
  48. public function assign($name, $value = null)
  49. {
  50. if (is_array($name)) {
  51. $this->data = array_merge($this->data, $name);
  52. } else {
  53. $this->data[$name] = $value;
  54. }
  55. return $this;
  56. }
  57. /**
  58. * 视图过滤
  59. * @access public
  60. * @param Callable $filter 过滤方法或闭包
  61. * @return $this
  62. */
  63. public function filter(callable $filter = null)
  64. {
  65. $this->filter = $filter;
  66. return $this;
  67. }
  68. /**
  69. * 解析和获取模板内容 用于输出
  70. * @access public
  71. * @param string $template 模板文件名或者内容
  72. * @param array $vars 模板变量
  73. * @return string
  74. * @throws \Exception
  75. */
  76. public function fetch(string $template = '', array $vars = []): string
  77. {
  78. return $this->getContent(function () use ($vars, $template) {
  79. $this->engine()->fetch($template, array_merge($this->data, $vars));
  80. });
  81. }
  82. /**
  83. * 渲染内容输出
  84. * @access public
  85. * @param string $content 内容
  86. * @param array $vars 模板变量
  87. * @return string
  88. */
  89. public function display(string $content, array $vars = []): string
  90. {
  91. return $this->getContent(function () use ($vars, $content) {
  92. $this->engine()->display($content, array_merge($this->data, $vars));
  93. });
  94. }
  95. /**
  96. * 获取模板引擎渲染内容
  97. * @param $callback
  98. * @return string
  99. * @throws \Exception
  100. */
  101. protected function getContent($callback): string
  102. {
  103. // 页面缓存
  104. ob_start();
  105. if (PHP_VERSION > 8.0) {
  106. ob_implicit_flush(false);
  107. } else {
  108. ob_implicit_flush(0);
  109. }
  110. // 渲染输出
  111. try {
  112. $callback();
  113. } catch (\Exception $e) {
  114. ob_end_clean();
  115. throw $e;
  116. }
  117. // 获取并清空缓存
  118. $content = ob_get_clean();
  119. if ($this->filter) {
  120. $content = call_user_func_array($this->filter, [$content]);
  121. }
  122. return $content;
  123. }
  124. /**
  125. * 模板变量赋值
  126. * @access public
  127. * @param string $name 变量名
  128. * @param mixed $value 变量值
  129. */
  130. public function __set($name, $value)
  131. {
  132. $this->data[$name] = $value;
  133. }
  134. /**
  135. * 取得模板显示变量的值
  136. * @access protected
  137. * @param string $name 模板变量
  138. * @return mixed
  139. */
  140. public function __get($name)
  141. {
  142. return $this->data[$name];
  143. }
  144. /**
  145. * 检测模板变量是否设置
  146. * @access public
  147. * @param string $name 模板变量名
  148. * @return bool
  149. */
  150. public function __isset($name)
  151. {
  152. return isset($this->data[$name]);
  153. }
  154. protected function resolveConfig(string $name)
  155. {
  156. $config = $this->app->config->get('view', []);
  157. Arr::forget($config, 'type');
  158. return $config;
  159. }
  160. /**
  161. * 默认驱动
  162. * @return string|null
  163. */
  164. public function getDefaultDriver()
  165. {
  166. return $this->app->config->get('view.type', 'php');
  167. }
  168. }