diff --git a/library/think/Template.php b/library/think/Template.php index 3f07e510..426367ab 100644 --- a/library/think/Template.php +++ b/library/think/Template.php @@ -324,10 +324,8 @@ class Template */ private function parsePhp(&$content) { - if (ini_get('short_open_tag')) { - // 开启短标签的情况要将' . "\n", $content); - } + // 短标签的情况要将' . "\n", $content); // PHP语法检查 if ($this->config['tpl_deny_php'] && false !== strpos($content, '(?:(?!' . $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': $begin = $this->config['tpl_begin']; $end = $this->config['tpl_end']; if (strlen(ltrim($begin, '\\')) == 1 && strlen(ltrim($end, '\\')) == 1) { - $regex = $begin . '((?:(?:[\$]{1,2}|[\:\~][\$a-wA-w_]|[+]{2}[\$]|[-]{2}[\$])[\w\.\:\[\(\*\/\-\+\%_]|\/[\*\/])(?>[^' . $end . ']*))' . $end; + $regex = $begin . '((?:[\$]{1,2}[a-wA-w_]|[\:\~][\$a-wA-w_]|[+]{2}[\$][a-wA-w_]|[-]{2}[\$][a-wA-w_]|\/[\*\/])(?>[^' . $end . ']*))' . $end; } else { - $regex = $begin . '((?:(?:[\$]{1,2}|[\:\~][\$a-wA-w_]|[+]{2}[\$]|[-]{2}[\$])[\w\.\:\[\(\*\/\-\+\%_]|\/[\*\/])(?>(?:(?!' . $end . ').)*))' . $end; + $regex = $begin . '((?:[\$]{1,2}[a-wA-w_]|[\:\~][\$a-wA-w_]|[+]{2}[\$][a-wA-w_]|[-]{2}[\$][a-wA-w_]|\/[\*\/])(?>(?:(?!' . $end . ').)*))' . $end; } break; } diff --git a/library/think/template/taglib/Cx.php b/library/think/template/taglib/Cx.php index 27755697..4b836b01 100644 --- a/library/think/template/taglib/Cx.php +++ b/library/think/template/taglib/Cx.php @@ -81,6 +81,8 @@ class Cx extends Taglib $empty = isset($tag['empty']) ? $tag['empty'] : ''; $key = !empty($tag['key']) ? $tag['key'] : 'i'; $mod = isset($tag['mod']) ? $tag['mod'] : '2'; + $offset = !empty($tag['offset']) && is_numeric($tag['offset']) ? intval($tag['offset']) : 0; + $length = !empty($tag['length']) && is_numeric($tag['length']) ? intval($tag['length']) : 'null'; // 允许使用函数设定数据集 {$vo.name} $parseStr = 'autoBuildVar($name); } $parseStr .= 'if(is_array(' . $name . ')): $' . $key . ' = 0;'; - if (isset($tag['length']) && '' != $tag['length']) { - $parseStr .= ' $__LIST__ = array_slice(' . $name . ',' . $tag['offset'] . ',' . $tag['length'] . ',true);'; - } elseif (isset($tag['offset']) && '' != $tag['offset']) { - $parseStr .= ' $__LIST__ = array_slice(' . $name . ',' . $tag['offset'] . ',null,true);'; + // 设置了输出数组长度 + if (0 != $offset || 'null' != $length) { + $parseStr .= '$__LIST__ = array_slice(' . $name . ',' . $offset . ',' . $length . ', true); '; } else { $parseStr .= ' $__LIST__ = ' . $name . ';'; } @@ -153,7 +154,7 @@ class Cx extends Taglib if (!isset($var)) { $var = '$_' . uniqid(); } - $parseStr .= $var . ' = array_slice(' . $name . ',' . $offset . ',' . $length . ',true); '; + $parseStr .= $var . ' = array_slice(' . $name . ',' . $offset . ',' . $length . ', true); '; } else { $var = &$name; } diff --git a/tests/thinkphp/library/think/template/taglib/cxTest.php b/tests/thinkphp/library/think/template/taglib/cxTest.php index be421efc..b3de4fb8 100644 --- a/tests/thinkphp/library/think/template/taglib/cxTest.php +++ b/tests/thinkphp/library/think/template/taglib/cxTest.php @@ -55,12 +55,12 @@ EOF; $this->assertEquals($data, $content); $content = <<fetch($content); + $template->fetch($content, ['list' => [1,2,3,4,5]]); $this->expectOutputString('234'); } @@ -96,7 +96,7 @@ EOF; $this->assertEquals($content, $data); $content = << + default EOF; diff --git a/tests/thinkphp/library/think/templateTest.php b/tests/thinkphp/library/think/templateTest.php index b3486714..66c741db 100644 --- a/tests/thinkphp/library/think/templateTest.php +++ b/tests/thinkphp/library/think/templateTest.php @@ -104,6 +104,16 @@ EOF; $template->parse($content); $this->assertEquals($data, $content); + $content = << +EOF; + + $template->parse($content); + $this->assertEquals($data, $content); + $content = <<parse($content); $this->assertEquals($data, $content); + $content = <<parse($content); + $this->assertEquals($data, $content); + } public function testVarFunction() @@ -169,10 +187,12 @@ EOF; $this->assertEquals($data, $content); $content = << + + EOF; $template->parse($content); @@ -189,7 +209,7 @@ EOF; $content = << EOF; - $data = <<a) ? (is_array(\$info)?\$info['a']:\$info->a) : 'test'; ?> EOF; @@ -199,7 +219,7 @@ EOF; $content = << EOF; - $data = <<a)==(is_array(\$info)?\$info['b']:\$info->b)) echo 'test'; ?> EOF; @@ -209,11 +229,22 @@ EOF; $content = << EOF; - $data = <<a) !== ''?(is_array(\$info)?\$info['a']:\$info->a):'test')?'yes':'no'; ?> EOF; $template->parse($content); $this->assertEquals($data, $content); + + $template2 = new Template(); + $template2->tpl_var_identify = 'obj'; + $content = <<b)?'yes':'no'; ?> +EOF; + $template2->parse($content); + $this->assertEquals($data, $content); } public function testTag() @@ -278,7 +309,8 @@ EOF; {\$Think.VERSION}
{\$Think.LDELIM}
{\$Think.RDELIM}
-{\$Think.SITE_NAME} +{\$Think.SITE_NAME}
+{\$Think.SITE.URL} EOF; $data = <<
@@ -297,9 +329,44 @@ EOF;


- +
+ EOF; $template->parse($content); $this->assertEquals($data, $content); } + + public function testDisplay() + { + $template = new Template(); + $template->assign('name', 'name'); + $config = [ + 'strip_space' => true, + 'tpl_path' => dirname(__FILE__) . '/', + ]; + $data = ['name' => 'value']; + $template->display('display', $data, $config); + $this->expectOutputString('value'); + } + + public function testFetch() + { + $template = new Template(); + $template->tpl_path = dirname(__FILE__) . '/'; + $data = ['name' => 'value']; + $content = <<fetch($content, $data); + $this->expectOutputString('value'); + } + + public function testVarAssign() + { + $template = new Template(); + $template->assign('name', 'value'); + $value = $template->get('name'); + $this->assertEquals('value', $value); + } }