Skip to content

Commit 7c74dd9

Browse files
committed
改进view类 调整模板引擎的配置参数定义和传入方式
1 parent 6e79350 commit 7c74dd9

File tree

5 files changed

+62
-96
lines changed

5 files changed

+62
-96
lines changed

convention.php

+20-17
Original file line numberDiff line numberDiff line change
@@ -95,23 +95,26 @@
9595
'url_action_convert' => true,
9696

9797
// +----------------------------------------------------------------------
98-
// | 视图及模板设置
99-
// +----------------------------------------------------------------------
100-
101-
'view' => [
102-
// 模板引擎
103-
'engine_type' => 'think',
104-
// 模板引擎配置
105-
'engine_config' => [
106-
// 模板路径
107-
'view_path' => '',
108-
// 模板后缀
109-
'view_suffix' => '.html',
110-
// 模板文件名分隔符
111-
'view_depr' => DS,
112-
],
113-
// 输出字符串替换
114-
'parse_str' => [],
98+
// | 模板引擎设置
99+
// +----------------------------------------------------------------------
100+
101+
'template' => [
102+
// 模板引擎类型 支持 php think 支持扩展
103+
'type' => 'Think',
104+
// 模板路径
105+
'view_path' => '',
106+
// 模板后缀
107+
'view_suffix' => '.html',
108+
// 模板文件名分隔符
109+
'view_depr' => DS,
110+
// 模板引擎普通标签开始标记
111+
'tpl_begin' => '{',
112+
// 模板引擎普通标签结束标记
113+
'tpl_end' => '}',
114+
// 标签库标签开始标记
115+
'taglib_begin' => '{',
116+
// 标签库标签结束标记
117+
'taglib_end' => '}',
115118
],
116119

117120
// 默认跳转页面对应的模板文件

helper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ function trace($log = '[think]', $level = 'log')
341341
*/
342342
function view($template = '', $vars = [])
343343
{
344-
return View::instance(Config::get('view'))->fetch($template, $vars);
344+
return View::instance(Config::get('template'))->fetch($template, $vars);
345345
}
346346

347347
/**

library/think/Controller.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Controller
3535
*/
3636
public function __construct()
3737
{
38-
$this->view = View::instance(Config::get('view'));
38+
$this->view = View::instance(Config::get('template'));
3939

4040
// 控制器初始化
4141
if (method_exists($this, '_initialize')) {

library/think/View.php

+38-56
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,32 @@ class View
1919
public $engine = null;
2020
// 模板变量
2121
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 = [];
3224

33-
public function __construct($config = [])
25+
/**
26+
* 架构函数
27+
* @access public
28+
* @param array $engine 模板引擎参数
29+
*/
30+
public function __construct($engine = [])
3431
{
35-
if (is_array($config)) {
36-
$this->config($config);
37-
}
38-
3932
// 初始化模板引擎
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);
4235
}
4336
}
4437

4538
/**
4639
* 初始化视图
4740
* @access public
48-
* @param array $config 配置参数
41+
* @param array $engine 模板引擎参数
4942
* @return object
5043
*/
51-
public static function instance($config = [])
44+
public static function instance($engine = [])
5245
{
5346
if (is_null(self::$instance)) {
54-
self::$instance = new self($config);
47+
self::$instance = new self($engine);
5548
}
5649
return self::$instance;
5750
}
@@ -74,41 +67,18 @@ public function assign($name, $value = '')
7467
return $this;
7568
}
7669

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-
10170
/**
10271
* 设置当前模板解析的引擎
10372
* @access public
104-
* @param string $engine 引擎名称
105-
* @param array $config 引擎参数
73+
* @param array $options 引擎参数
10674
* @return $this
10775
*/
108-
public function engine($engine, array $config = [])
76+
public function engine(array $options = [])
10977
{
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);
11282
return $this;
11383
}
11484

@@ -139,17 +109,29 @@ public function fetch($template = '', $vars = [], $config = [], $renderContent =
139109
// 内容过滤标签
140110
APP_HOOK && Hook::listen('view_filter', $content);
141111
// 允许用户自定义模板的字符串替换
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);
149114
}
150115
return $content;
151116
}
152117

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+
153135
/**
154136
* 渲染内容输出
155137
* @access public

tests/thinkphp/library/think/viewTest.php

+2-21
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,6 @@ public function testAssign()
4646
$this->assertAttributeEquals($expect_data, 'data', $view_instance);
4747
}
4848

49-
/**
50-
* 测试配置
51-
* @return mixed
52-
* @access public
53-
*/
54-
public function testConfig()
55-
{
56-
$view_instance = \think\View::instance([]);
57-
$data = $view_instance->config('key2', 'value2');
58-
$data = $view_instance->config('key3', 'value3');
59-
$data = $view_instance->config('key3', 'value_cover');
60-
//基础配置替换
61-
$data = $view_instance->config(array('engine_type' => 'php'));
62-
//目标结果
63-
$this->assertAttributeContains('value2', "config", $view_instance);
64-
$this->assertAttributeContains('value_cover', "config", $view_instance);
65-
$this->assertAttributeContains('php', "config", $view_instance);
66-
}
67-
6849
/**
6950
* 测试引擎设置
7051
* @return mixed
@@ -73,11 +54,11 @@ public function testConfig()
7354
public function testEngine()
7455
{
7556
$view_instance = \think\View::instance();
76-
$data = $view_instance->engine('php');
57+
$data = $view_instance->engine(['type' => 'php', 'view_path' => '', 'view_suffix' => '.php', 'view_depr' => DS]);
7758
$php_engine = new \think\view\driver\Php(['view_path' => '', 'view_suffix' => '.php', 'view_depr' => DS]);
7859
$this->assertAttributeEquals($php_engine, 'engine', $view_instance);
7960
//测试模板引擎驱动
80-
$data = $view_instance->engine('think');
61+
$data = $view_instance->engine(['type' => 'think', 'view_path' => '', 'view_suffix' => '.html', 'view_depr' => DS]);
8162
$think_engine = new \think\view\driver\Think(['view_path' => '', 'view_suffix' => '.html', 'view_depr' => DS]);
8263
$this->assertAttributeEquals($think_engine, 'engine', $view_instance);
8364
}

0 commit comments

Comments
 (0)