View.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 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((array) $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. $this->engine->$method($template, $vars, $config);
  149. } catch (\Exception $e) {
  150. ob_end_clean();
  151. throw $e;
  152. }
  153. // 获取并清空缓存
  154. $content = ob_get_clean();
  155. // 内容过滤标签
  156. Hook::listen('view_filter', $content);
  157. // 允许用户自定义模板的字符串替换
  158. $replace = array_merge($this->replace, $replace);
  159. if (!empty($replace)) {
  160. $content = strtr($content, $replace);
  161. }
  162. return $content;
  163. }
  164. /**
  165. * 视图内容替换
  166. * @access public
  167. * @param string|array $content 被替换内容(支持批量替换)
  168. * @param string $replace 替换内容
  169. * @return $this
  170. */
  171. public function replace($content, $replace = '')
  172. {
  173. if (is_array($content)) {
  174. $this->replace = array_merge($this->replace, $content);
  175. } else {
  176. $this->replace[$content] = $replace;
  177. }
  178. return $this;
  179. }
  180. /**
  181. * 渲染内容输出
  182. * @access public
  183. * @param string $content 内容
  184. * @param array $vars 模板输出变量
  185. * @param array $replace 替换内容
  186. * @param array $config 模板参数
  187. * @return mixed
  188. */
  189. public function display($content, $vars = [], $replace = [], $config = [])
  190. {
  191. return $this->fetch($content, $vars, $replace, $config, true);
  192. }
  193. /**
  194. * 模板变量赋值
  195. * @access public
  196. * @param string $name 变量名
  197. * @param mixed $value 变量值
  198. */
  199. public function __set($name, $value)
  200. {
  201. $this->data[$name] = $value;
  202. }
  203. /**
  204. * 取得模板显示变量的值
  205. * @access protected
  206. * @param string $name 模板变量
  207. * @return mixed
  208. */
  209. public function __get($name)
  210. {
  211. return $this->data[$name];
  212. }
  213. /**
  214. * 检测模板变量是否设置
  215. * @access public
  216. * @param string $name 模板变量名
  217. * @return bool
  218. */
  219. public function __isset($name)
  220. {
  221. return isset($this->data[$name]);
  222. }
  223. }