改进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

@@ -95,23 +95,26 @@ return [
'url_action_convert' => true,
// +----------------------------------------------------------------------
// | 视图及模板设置
// | 模板引擎设置
// +----------------------------------------------------------------------
'view' => [
// 模板引擎
'engine_type' => 'think',
// 模板引擎配置
'engine_config' => [
// 模板路径
'view_path' => '',
// 模板后缀
'view_suffix' => '.html',
// 模板文件名分隔符
'view_depr' => DS,
],
// 输出字符串替换
'parse_str' => [],
'template' => [
// 模板引擎类型 支持 php think 支持扩展
'type' => 'Think',
// 模板路径
'view_path' => '',
// 模板后缀
'view_suffix' => '.html',
// 模板文件名分隔符
'view_depr' => DS,
// 模板引擎普通标签开始标记
'tpl_begin' => '{',
// 模板引擎普通标签结束标记
'tpl_end' => '}',
// 标签库标签开始标记
'taglib_begin' => '{',
// 标签库标签结束标记
'taglib_end' => '}',
],
// 默认跳转页面对应的模板文件

View File

@@ -341,7 +341,7 @@ function trace($log = '[think]', $level = 'log')
*/
function view($template = '', $vars = [])
{
return View::instance(Config::get('view'))->fetch($template, $vars);
return View::instance(Config::get('template'))->fetch($template, $vars);
}
/**

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

View File

@@ -46,25 +46,6 @@ class viewTest extends \PHPUnit_Framework_TestCase
$this->assertAttributeEquals($expect_data, 'data', $view_instance);
}
/**
* 测试配置
* @return mixed
* @access public
*/
public function testConfig()
{
$view_instance = \think\View::instance([]);
$data = $view_instance->config('key2', 'value2');
$data = $view_instance->config('key3', 'value3');
$data = $view_instance->config('key3', 'value_cover');
//基础配置替换
$data = $view_instance->config(array('engine_type' => 'php'));
//目标结果
$this->assertAttributeContains('value2', "config", $view_instance);
$this->assertAttributeContains('value_cover', "config", $view_instance);
$this->assertAttributeContains('php', "config", $view_instance);
}
/**
* 测试引擎设置
* @return mixed
@@ -73,11 +54,11 @@ class viewTest extends \PHPUnit_Framework_TestCase
public function testEngine()
{
$view_instance = \think\View::instance();
$data = $view_instance->engine('php');
$data = $view_instance->engine(['type' => 'php', 'view_path' => '', 'view_suffix' => '.php', 'view_depr' => DS]);
$php_engine = new \think\view\driver\Php(['view_path' => '', 'view_suffix' => '.php', 'view_depr' => DS]);
$this->assertAttributeEquals($php_engine, 'engine', $view_instance);
//测试模板引擎驱动
$data = $view_instance->engine('think');
$data = $view_instance->engine(['type' => 'think', 'view_path' => '', 'view_suffix' => '.html', 'view_depr' => DS]);
$think_engine = new \think\view\driver\Think(['view_path' => '', 'view_suffix' => '.html', 'view_depr' => DS]);
$this->assertAttributeEquals($think_engine, 'engine', $view_instance);
}