0
0

View.php 2.8 KB

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