View.php
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\response;
use think\Config;
use think\Response;
use think\View as ViewTemplate;
class View extends Response
{
// 输出参数
protected $options = [];
protected $vars = [];
protected $replace = [];
protected $contentType = 'text/html';
/**
* 处理数据
* @access protected
* @param mixed $data 要处理的数据
* @return mixed
*/
protected function output($data)
{
// 渲染模板输出
return ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
->fetch($data, $this->vars, $this->replace);
}
/**
* 获取视图变量
* @access public
* @param string $name 模板变量
* @return mixed
*/
public function getVars($name = null)
{
if (is_null($name)) {
return $this->vars;
} else {
return isset($this->vars[$name]) ? $this->vars[$name] : null;
}
}
/**
* 模板变量赋值
* @access public
* @param mixed $name 变量名
* @param mixed $value 变量值
* @return $this
*/
public function assign($name, $value = '')
{
if (is_array($name)) {
$this->vars = array_merge($this->vars, $name);
return $this;
} else {
$this->vars[$name] = $value;
}
return $this;
}
/**
* 视图内容替换
* @access public
* @param string|array $content 被替换内容(支持批量替换)
* @param string $replace 替换内容
* @return $this
*/
public function replace($content, $replace = '')
{
if (is_array($content)) {
$this->replace = array_merge($this->replace, $content);
} else {
$this->replace[$content] = $replace;
}
return $this;
}
}