View.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 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. ob_implicit_flush(0);
  106. // 渲染输出
  107. try {
  108. $callback();
  109. } catch (\Exception $e) {
  110. ob_end_clean();
  111. throw $e;
  112. }
  113. // 获取并清空缓存
  114. $content = ob_get_clean();
  115. if ($this->filter) {
  116. $content = call_user_func_array($this->filter, [$content]);
  117. }
  118. return $content;
  119. }
  120. /**
  121. * 模板变量赋值
  122. * @access public
  123. * @param string $name 变量名
  124. * @param mixed $value 变量值
  125. */
  126. public function __set($name, $value)
  127. {
  128. $this->data[$name] = $value;
  129. }
  130. /**
  131. * 取得模板显示变量的值
  132. * @access protected
  133. * @param string $name 模板变量
  134. * @return mixed
  135. */
  136. public function __get($name)
  137. {
  138. return $this->data[$name];
  139. }
  140. /**
  141. * 检测模板变量是否设置
  142. * @access public
  143. * @param string $name 模板变量名
  144. * @return bool
  145. */
  146. public function __isset($name)
  147. {
  148. return isset($this->data[$name]);
  149. }
  150. protected function resolveConfig(string $name)
  151. {
  152. $config = $this->app->config->get('view', []);
  153. Arr::forget($config, 'type');
  154. return $config;
  155. }
  156. /**
  157. * 默认驱动
  158. * @return string|null
  159. */
  160. public function getDefaultDriver()
  161. {
  162. return $this->app->config->get('view.type', 'php');
  163. }
  164. }