改进view类 调整模板引擎的配置参数定义和传入方式

This commit is contained in:
thinkphp
2016-04-26 17:00:55 +08:00
parent 6e79350a79
commit 7c74dd9591
5 changed files with 60 additions and 94 deletions

View File

@@ -35,7 +35,7 @@ class Controller
*/
public function __construct()
{
$this->view = View::instance(Config::get('view'));
$this->view = View::instance(Config::get('template'));
// 控制器初始化
if (method_exists($this, '_initialize')) {

View File

@@ -19,39 +19,32 @@ class View
public $engine = null;
// 模板变量
protected $data = [];
// 视图参数
protected $config = [
// 视图输出字符串替换
'parse_str' => [],
// 视图驱动命名空间
'namespace' => '\\think\\view\\driver\\',
'engine_type' => 'think',
// 模板引擎配置参数
'engine_config' => [],
];
// 视图替换
protected $replace = [];
public function __construct($config = [])
/**
* 架构函数
* @access public
* @param array $engine 模板引擎参数
*/
public function __construct($engine = [])
{
if (is_array($config)) {
$this->config($config);
}
// 初始化模板引擎
if (!empty($this->config['engine_type'])) {
$this->engine($this->config['engine_type'], $this->config['engine_config']);
if (!empty($engine)) {
$this->engine($engine);
}
}
/**
* 初始化视图
* @access public
* @param array $config 配置参数
* @param array $engine 模板引擎参数
* @return object
*/
public static function instance($config = [])
public static function instance($engine = [])
{
if (is_null(self::$instance)) {
self::$instance = new self($config);
self::$instance = new self($engine);
}
return self::$instance;
}
@@ -74,41 +67,18 @@ class View
return $this;
}
/**
* 设置视图参数
* @access public
* @param mixed $config 视图参数或者数组
* @param string $value 值
* @return mixed
*/
public function config($config = '', $value = null)
{
if (is_array($config)) {
foreach ($this->config as $key => $val) {
if (isset($config[$key])) {
$this->config[$key] = $config[$key];
}
}
} elseif (is_null($value)) {
// 获取配置参数
return $this->config[$config];
} else {
$this->config[$config] = $value;
}
return $this;
}
/**
* 设置当前模板解析的引擎
* @access public
* @param string $engine 引擎名称
* @param array $config 引擎参数
* @param array $options 引擎参数
* @return $this
*/
public function engine($engine, array $config = [])
public function engine(array $options = [])
{
$class = $this->config['namespace'] . ucfirst($engine);
$this->engine = new $class($config);
$type = !empty($options['type']) ? $options['type'] : 'Think';
$class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\view\\driver\\') . ucfirst($type);
unset($options['type']);
$this->engine = new $class($options);
return $this;
}
@@ -139,17 +109,29 @@ class View
// 内容过滤标签
APP_HOOK && Hook::listen('view_filter', $content);
// 允许用户自定义模板的字符串替换
if (!empty($this->config['parse_str'])) {
$replace = $this->config['parse_str'];
$content = str_replace(array_keys($replace), array_values($replace), $content);
}
if (!Config::get('response_auto_output')) {
// 自动响应输出
return Response::send($content, Response::type());
if (!empty($this->replace)) {
$content = str_replace(array_keys($this->replace), array_values($this->replace), $content);
}
return $content;
}
/**
* 视图内容替换
* @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;
}
/**
* 渲染内容输出
* @access public