View.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\response;
  13. use think\Cookie;
  14. use think\Response;
  15. use think\View as BaseView;
  16. /**
  17. * View Response
  18. */
  19. class View extends Response
  20. {
  21. /**
  22. * 输出参数
  23. * @var array
  24. */
  25. protected $options = [];
  26. /**
  27. * 输出变量
  28. * @var array
  29. */
  30. protected $vars = [];
  31. /**
  32. * 输出过滤
  33. * @var mixed
  34. */
  35. protected $filter;
  36. /**
  37. * 输出type
  38. * @var string
  39. */
  40. protected $contentType = 'text/html';
  41. /**
  42. * View对象
  43. * @var BaseView
  44. */
  45. protected $view;
  46. /**
  47. * 是否内容渲染
  48. * @var bool
  49. */
  50. protected $isContent = false;
  51. public function __construct(Cookie $cookie, BaseView $view, $data = '', int $code = 200)
  52. {
  53. $this->init($data, $code);
  54. $this->cookie = $cookie;
  55. $this->view = $view;
  56. }
  57. /**
  58. * 设置是否为内容渲染
  59. * @access public
  60. * @param bool $content
  61. * @return $this
  62. */
  63. public function isContent(bool $content = true)
  64. {
  65. $this->isContent = $content;
  66. return $this;
  67. }
  68. /**
  69. * 处理数据
  70. * @access protected
  71. * @param mixed $data 要处理的数据
  72. * @return string
  73. */
  74. protected function output($data): string
  75. {
  76. // 渲染模板输出
  77. return $this->view->filter($this->filter)
  78. ->fetch($data, $this->vars, $this->isContent);
  79. }
  80. /**
  81. * 获取视图变量
  82. * @access public
  83. * @param string $name 模板变量
  84. * @return mixed
  85. */
  86. public function getVars(string $name = null)
  87. {
  88. if (is_null($name)) {
  89. return $this->vars;
  90. } else {
  91. return $this->vars[$name] ?? null;
  92. }
  93. }
  94. /**
  95. * 模板变量赋值
  96. * @access public
  97. * @param string|array $name 模板变量
  98. * @param mixed $value 变量值
  99. * @return $this
  100. */
  101. public function assign($name, $value = null)
  102. {
  103. if (is_array($name)) {
  104. $this->vars = array_merge($this->vars, $name);
  105. } else {
  106. $this->vars[$name] = $value;
  107. }
  108. return $this;
  109. }
  110. /**
  111. * 视图内容过滤
  112. * @access public
  113. * @param callable $filter
  114. * @return $this
  115. */
  116. public function filter(callable $filter = null)
  117. {
  118. $this->filter = $filter;
  119. return $this;
  120. }
  121. /**
  122. * 检查模板是否存在
  123. * @access public
  124. * @param string $name 模板名
  125. * @return bool
  126. */
  127. public function exists(string $name): bool
  128. {
  129. return $this->view->exists($name);
  130. }
  131. }