From 06ba891ca401fe0591a2e888318cdcef7a3a7d50 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 14 Mar 2016 14:02:37 +0800 Subject: [PATCH] =?UTF-8?q?View=E7=B1=BB=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Template.php | 24 ++++++++-------- library/think/View.php | 35 ++++------------------- tests/thinkphp/library/think/viewTest.php | 12 -------- 3 files changed, 18 insertions(+), 53 deletions(-) diff --git a/library/think/Template.php b/library/think/Template.php index 41f64c18..55f2cea0 100644 --- a/library/think/Template.php +++ b/library/think/Template.php @@ -712,31 +712,31 @@ class Template } elseif (')' == substr($name, -1, 1)) { // $name为对象或是自动识别,或者含有函数 switch ($first) { - case '?': + case '?': $str = ''; break; - case '=': + case '=': $str = ''; break; - default: + default: $str = ''; } } else { // $name为数组 switch ($first) { - case '?': + case '?': // {$varname??'xxx'} $varname有定义则输出$varname,否则输出xxx $str = ''; break; - case '=': + case '=': // {$varname?='xxx'} $varname为真时才输出xxx $str = ''; break; - case ':': + case ':': // {$varname?:'xxx'} $varname为真时输出$varname,否则输出xxx $str = ''; break; - default: + default: if (strpos($str, ':')) { // {$varname ? 'a' : 'b'} $varname为真时输出a,否则输出b $str = ''; @@ -814,13 +814,13 @@ class Template $parseStr = $this->parseThinkVar($vars); } else { switch ($this->config['tpl_var_identify']) { - case 'array': // 识别为数组 + case 'array': // 识别为数组 $parseStr = $first . '[\'' . implode('\'][\'', $vars) . '\']'; break; - case 'obj': // 识别为对象 + case 'obj': // 识别为对象 $parseStr = $first . '->' . implode('->', $vars); break; - default: // 自动判断数组或对象 + default: // 自动判断数组或对象 $parseStr = '(is_array(' . $first . ')?' . $first . '[\'' . implode('\'][\'', $vars) . '\']:' . $first . '->' . implode('->', $vars) . ')'; } } @@ -866,14 +866,14 @@ class Template // 模板函数过滤 $fun = trim($args[0]); switch ($fun) { - case 'default': // 特殊模板函数 + case 'default': // 特殊模板函数 if (false === strpos($name, '(')) { $name = '(isset(' . $name . ') && (' . $name . ' !== \'\')?' . $name . ':' . $args[1] . ')'; } else { $name = '(' . $name . ' !== \'\'?' . $name . ':' . $args[1] . ')'; } break; - default: // 通用模板函数 + default: // 通用模板函数 if (!in_array($fun, $template_deny_funs)) { if (isset($args[1])) { if (strstr($args[1], '###')) { diff --git a/library/think/View.php b/library/think/View.php index 25ce9612..0c623333 100644 --- a/library/think/View.php +++ b/library/think/View.php @@ -242,15 +242,12 @@ class View } $depr = $this->config['view_depr']; $template = str_replace(['/', ':'], $depr, $template); - - // 获取当前模块 - $module = defined('MODULE_NAME') ? MODULE_NAME : ''; + $theme = $this->getTemplateTheme(); + $path = $this->config['view_path']; if (strpos($template, '@')) { - // 跨模块调用模版文件 list($module, $template) = explode('@', $template); + $path = APP_PATH . (APP_MULTI_MODULE ? $module . DS : '') . $this->config['view_layer'] . DS; } - // 获取当前主题的模版路径 - $path = $this->getThemePath($module); // 分析模板文件规则 if (defined('CONTROLLER_NAME')) { @@ -261,16 +258,15 @@ class View $template = str_replace('.', DS, CONTROLLER_NAME) . $depr . $template; } } - return $path . $template . $this->config['view_suffix']; + return realpath($path) . DS . $theme . $template . $this->config['view_suffix']; } /** * 获取当前的模板主题 * @access private - * @param string $module 模块名 * @return string */ - private function getTemplateTheme($module) + private function getTemplateTheme() { if ($this->config['theme_on']) { if ($this->theme) { @@ -284,7 +280,7 @@ class View } elseif (Cookie::get('think_theme')) { $theme = Cookie::get('think_theme'); } - if (!isset($theme) || !is_dir(APP_PATH . (APP_MULTI_MODULE ? $module . DS : '') . $this->config['view_layer'] . DS . $theme)) { + if (!isset($theme) || !is_dir($this->config['view_path'] . DS . $theme)) { $theme = $this->config['default_theme']; } Cookie::set('think_theme', $theme, 864000); @@ -296,25 +292,6 @@ class View return ''; } - /** - * 获取当前的模板路径 - * @access protected - * @param string $module 模块名 - * @return string - */ - protected function getThemePath($module = '') - { - // 获取当前主题名称 - $theme = $this->getTemplateTheme($module); - // 获取当前主题的模版路径 - $tmplPath = $this->config['view_path']; // 模块设置独立的视图目录 - if (!$tmplPath) { - // 定义TMPL_PATH 则改变全局的视图目录到模块之外 - $tmplPath = defined('TMPL_PATH') ? TMPL_PATH . $module . DS : APP_PATH . (APP_MULTI_MODULE ? $module . DS : '') . $this->config['view_layer'] . DS; - } - return realpath($tmplPath) . DS . $theme; - } - /** * 模板变量赋值 * @access public diff --git a/tests/thinkphp/library/think/viewTest.php b/tests/thinkphp/library/think/viewTest.php index db230764..891a25eb 100644 --- a/tests/thinkphp/library/think/viewTest.php +++ b/tests/thinkphp/library/think/viewTest.php @@ -132,16 +132,4 @@ class viewTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expect_data, $method->invoke($view_instance, 'template_name')); } - /** - * 测试引擎设置 - * @return mixed - * @access public - */ - public function testGetThemePath() - { - $view_instance = \think\View::instance(); - $method = new \ReflectionMethod('\think\View', 'getThemePath'); - $method->setAccessible(true); - $this->assertEquals(DS . 'theme_name' . DS, $method->invoke($view_instance)); - } }