From 7c74dd9591ab7eaad393d24a5dae41db73b8c20a Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 26 Apr 2016 17:00:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Bview=E7=B1=BB=20=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E6=A8=A1=E6=9D=BF=E5=BC=95=E6=93=8E=E7=9A=84=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=8F=82=E6=95=B0=E5=AE=9A=E4=B9=89=E5=92=8C=E4=BC=A0?= =?UTF-8?q?=E5=85=A5=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- convention.php | 33 ++++---- helper.php | 2 +- library/think/Controller.php | 2 +- library/think/View.php | 94 +++++++++-------------- tests/thinkphp/library/think/viewTest.php | 23 +----- 5 files changed, 60 insertions(+), 94 deletions(-) diff --git a/convention.php b/convention.php index 4bb5f76d..4f74f7f9 100644 --- a/convention.php +++ b/convention.php @@ -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' => '}', ], // 默认跳转页面对应的模板文件 diff --git a/helper.php b/helper.php index ad920885..a46259bf 100644 --- a/helper.php +++ b/helper.php @@ -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); } /** diff --git a/library/think/Controller.php b/library/think/Controller.php index efc0b698..618d67f7 100644 --- a/library/think/Controller.php +++ b/library/think/Controller.php @@ -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')) { diff --git a/library/think/View.php b/library/think/View.php index 1afeff76..2ff2be44 100644 --- a/library/think/View.php +++ b/library/think/View.php @@ -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 diff --git a/tests/thinkphp/library/think/viewTest.php b/tests/thinkphp/library/think/viewTest.php index 3afcf293..c8efaee5 100644 --- a/tests/thinkphp/library/think/viewTest.php +++ b/tests/thinkphp/library/think/viewTest.php @@ -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); }