@@ -19,39 +19,32 @@ class View
19
19
public $ engine = null ;
20
20
// 模板变量
21
21
protected $ data = [];
22
- // 视图参数
23
- protected $ config = [
24
- // 视图输出字符串替换
25
- 'parse_str ' => [],
26
- // 视图驱动命名空间
27
- 'namespace ' => '\\think \\view \\driver \\' ,
28
- 'engine_type ' => 'think ' ,
29
- // 模板引擎配置参数
30
- 'engine_config ' => [],
31
- ];
22
+ // 视图替换
23
+ protected $ replace = [];
32
24
33
- public function __construct ($ config = [])
25
+ /**
26
+ * 架构函数
27
+ * @access public
28
+ * @param array $engine 模板引擎参数
29
+ */
30
+ public function __construct ($ engine = [])
34
31
{
35
- if (is_array ($ config )) {
36
- $ this ->config ($ config );
37
- }
38
-
39
32
// 初始化模板引擎
40
- if (!empty ($ this -> config [ ' engine_type ' ] )) {
41
- $ this ->engine ($ this -> config [ ' engine_type ' ], $ this -> config [ ' engine_config ' ] );
33
+ if (!empty ($ engine )) {
34
+ $ this ->engine ($ engine );
42
35
}
43
36
}
44
37
45
38
/**
46
39
* 初始化视图
47
40
* @access public
48
- * @param array $config 配置参数
41
+ * @param array $engine 模板引擎参数
49
42
* @return object
50
43
*/
51
- public static function instance ($ config = [])
44
+ public static function instance ($ engine = [])
52
45
{
53
46
if (is_null (self ::$ instance )) {
54
- self ::$ instance = new self ($ config );
47
+ self ::$ instance = new self ($ engine );
55
48
}
56
49
return self ::$ instance ;
57
50
}
@@ -74,41 +67,18 @@ public function assign($name, $value = '')
74
67
return $ this ;
75
68
}
76
69
77
- /**
78
- * 设置视图参数
79
- * @access public
80
- * @param mixed $config 视图参数或者数组
81
- * @param string $value 值
82
- * @return mixed
83
- */
84
- public function config ($ config = '' , $ value = null )
85
- {
86
- if (is_array ($ config )) {
87
- foreach ($ this ->config as $ key => $ val ) {
88
- if (isset ($ config [$ key ])) {
89
- $ this ->config [$ key ] = $ config [$ key ];
90
- }
91
- }
92
- } elseif (is_null ($ value )) {
93
- // 获取配置参数
94
- return $ this ->config [$ config ];
95
- } else {
96
- $ this ->config [$ config ] = $ value ;
97
- }
98
- return $ this ;
99
- }
100
-
101
70
/**
102
71
* 设置当前模板解析的引擎
103
72
* @access public
104
- * @param string $engine 引擎名称
105
- * @param array $config 引擎参数
73
+ * @param array $options 引擎参数
106
74
* @return $this
107
75
*/
108
- public function engine ($ engine , array $ config = [])
76
+ public function engine (array $ options = [])
109
77
{
110
- $ class = $ this ->config ['namespace ' ] . ucfirst ($ engine );
111
- $ this ->engine = new $ class ($ config );
78
+ $ type = !empty ($ options ['type ' ]) ? $ options ['type ' ] : 'Think ' ;
79
+ $ class = (!empty ($ options ['namespace ' ]) ? $ options ['namespace ' ] : '\\think \\view \\driver \\' ) . ucfirst ($ type );
80
+ unset($ options ['type ' ]);
81
+ $ this ->engine = new $ class ($ options );
112
82
return $ this ;
113
83
}
114
84
@@ -139,17 +109,29 @@ public function fetch($template = '', $vars = [], $config = [], $renderContent =
139
109
// 内容过滤标签
140
110
APP_HOOK && Hook::listen ('view_filter ' , $ content );
141
111
// 允许用户自定义模板的字符串替换
142
- if (!empty ($ this ->config ['parse_str ' ])) {
143
- $ replace = $ this ->config ['parse_str ' ];
144
- $ content = str_replace (array_keys ($ replace ), array_values ($ replace ), $ content );
145
- }
146
- if (!Config::get ('response_auto_output ' )) {
147
- // 自动响应输出
148
- return Response::send ($ content , Response::type ());
112
+ if (!empty ($ this ->replace )) {
113
+ $ content = str_replace (array_keys ($ this ->replace ), array_values ($ this ->replace ), $ content );
149
114
}
150
115
return $ content ;
151
116
}
152
117
118
+ /**
119
+ * 视图内容替换
120
+ * @access public
121
+ * @param string|array $content 被替换内容(支持批量替换)
122
+ * @param string $replace 替换内容
123
+ * @return $this
124
+ */
125
+ public function replace ($ content , $ replace = '' )
126
+ {
127
+ if (is_array ($ content )) {
128
+ $ this ->replace = array_merge ($ this ->replace , $ content );
129
+ } else {
130
+ $ this ->replace [$ content ] = $ replace ;
131
+ }
132
+ return $ this ;
133
+ }
134
+
153
135
/**
154
136
* 渲染内容输出
155
137
* @access public
0 commit comments