From 7c24df26ddf030addf50989f59174e2344815299 Mon Sep 17 00:00:00 2001 From: oldrind <1401019000@qq.com> Date: Thu, 4 Feb 2016 11:39:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=A8=A1=E6=9D=BF=E5=8C=85?= =?UTF-8?q?=E5=90=AB=E6=96=87=E4=BB=B6=E5=AF=B9=E5=8A=A8=E6=80=81=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Template.php | 37 +++++++++++++------ tests/thinkphp/library/think/templateTest.php | 14 ++++++- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/library/think/Template.php b/library/think/Template.php index c2cea9e1..315a0e28 100644 --- a/library/think/Template.php +++ b/library/think/Template.php @@ -132,11 +132,24 @@ class Template * 模板变量获取 * @access public * @param string $name 变量名 - * @return string|false + * @return mixed */ - public function get($name) + public function get($name = '') { - return isset($this->data[$name]) ? $this->data[$name] : false; + if ('' == $name) { + return $this->data; + } else { + $data = $this->data; + foreach (explode('.', $name) as $key => $val) { + if (isset($data[$val])) { + $data = $data[$val]; + } else { + $data = false; + break; + } + } + return $data; + } } /** @@ -896,19 +909,21 @@ class Template */ private function parseTemplateName($templateName) { - if ('$' == substr($templateName, 0, 1)) { - //支持加载变量文件名 - $templateName = $this->get(substr($templateName, 1)); - } $array = explode(',', $templateName); $parseStr = ''; foreach ($array as $templateName) { if (empty($templateName)) { continue; } + if (0 === strpos($templateName, '$')) { + //支持加载变量文件名 + $templateName = $this->get(substr($templateName, 1)); + } $template = $this->parseTemplateFile($templateName); - // 获取模板文件内容 - $parseStr .= file_get_contents($template); + if (is_file($template)) { + // 获取模板文件内容 + $parseStr .= file_get_contents($template); + } } return $parseStr; } @@ -973,9 +988,9 @@ class Template $name = 'name'; } if ($single) { - $regex = $begin . $tagName . '\b(?>(?:(?!' . $name . '=).)*)\b' . $name . '=([\'\"])(?[\w\-\/\.\:@,\\\\]+)\\1(?>[^' . $end . ']*)' . $end; + $regex = $begin . $tagName . '\b(?>(?:(?!' . $name . '=).)*)\b' . $name . '=([\'\"])(?[\$\w\-\/\.\:@,\\\\]+)\\1(?>[^' . $end . ']*)' . $end; } else { - $regex = $begin . $tagName . '\b(?>(?:(?!' . $name . '=).)*)\b' . $name . '=([\'\"])(?[\w\-\/\.\:@,\\\\]+)\\1(?>(?:(?!' . $end . ').)*)' . $end; + $regex = $begin . $tagName . '\b(?>(?:(?!' . $name . '=).)*)\b' . $name . '=([\'\"])(?[\$\w\-\/\.\:@,\\\\]+)\\1(?>(?:(?!' . $end . ').)*)' . $end; } break; case 'tag': diff --git a/tests/thinkphp/library/think/templateTest.php b/tests/thinkphp/library/think/templateTest.php index f01e6e4f..b1522197 100644 --- a/tests/thinkphp/library/think/templateTest.php +++ b/tests/thinkphp/library/think/templateTest.php @@ -252,11 +252,13 @@ EOF; $config['tpl_path'] = dirname(__FILE__) . '/'; $config['tpl_suffix'] = '.html'; $template = new Template($config); + $files = ['extend' => 'extend', 'include' => 'include']; + $template->assign('files', $files); $content = <<get('name'); $this->assertEquals('value', $value); } + + public function testVarGet() + { + $template = new Template(); + $data = ['a' => 'a', 'b' => 'b']; + $template->assign($data); + $this->assertEquals($data, $template->get()); + } }