diff --git a/library/think/Template.php b/library/think/Template.php index a0f27706..bada4d2f 100644 --- a/library/think/Template.php +++ b/library/think/Template.php @@ -556,9 +556,15 @@ class Template $children[$parent][] = $name; continue; } + } elseif (!empty($val['parent'])) { + // 如果子标签没有被继承则用原值 + $children[$val['parent']][] = $name; + $blocks[$name] = $val; + } + if (!$val['parent']) { + // 替换模板中的顶级block标签 + $extend = str_replace($val['begin'] . $val['content'] . $val['end'], $replace, $extend); } - // 替换模板中的block标签 - $extend = str_replace($val['begin'] . $val['content'] . $val['end'], $replace, $extend); } } $content = $extend; @@ -735,20 +741,15 @@ class Template $str = trim(substr($str, $pos + 1)); $this->parseVar($str); $first = substr($str, 0, 1); - if (isset($array[1])) { - $this->parseVar($array[2]); - $name .= $array[1] . $array[2]; - if ('=' == $first) { - // {$varname?='xxx'} $varname为真时才输出xxx - $str = ''; - } else { - $str = ''; - } - } elseif (')' == substr($name, -1, 1)) { + if (strpos($name, ')')) { // $name为对象或是自动识别,或者含有函数 + if (isset($array[1])) { + $this->parseVar($array[2]); + $name .= $array[1] . $array[2]; + } switch ($first) { case '?': - $str = ''; + $str = ''; break; case '=': $str = ''; @@ -757,26 +758,32 @@ class Template $str = ''; } } else { + if (isset($array[1])) { + $this->parseVar($array[2]); + $_name = ' && ' . $name . $array[1] . $array[2]; + } else { + $_name = ''; + } // $name为数组 switch ($first) { case '?': // {$varname??'xxx'} $varname有定义则输出$varname,否则输出xxx - $str = ''; + $str = ''; break; case '=': // {$varname?='xxx'} $varname为真时才输出xxx - $str = ''; + $str = ''; break; case ':': // {$varname?:'xxx'} $varname为真时输出$varname,否则输出xxx - $str = ''; + $str = ''; break; default: if (strpos($str, ':')) { // {$varname ? 'a' : 'b'} $varname为真时输出a,否则输出b - $str = ''; + $str = ''; } else { - $str = ''; + $str = ''; } } } diff --git a/tests/thinkphp/library/think/templateTest.php b/tests/thinkphp/library/think/templateTest.php index 9b21028f..df9aff65 100644 --- a/tests/thinkphp/library/think/templateTest.php +++ b/tests/thinkphp/library/think/templateTest.php @@ -78,7 +78,7 @@ EOF; {\$name.a==\$name.b?='test'} EOF; $data = << + EOF; $template->parse($content); @@ -88,7 +88,7 @@ EOF; {\$name.a==\$name.b?'a':'b'} EOF; $data = << + EOF; $template->parse($content); @@ -98,7 +98,7 @@ EOF; {\$name.a|default='test'==\$name.b?'a':'b'} EOF; $data = << + EOF; $template->parse($content); @@ -210,7 +210,7 @@ EOF; <#\$info.a??'test'#> EOF; $data = <<a) ? (is_array(\$info)?\$info['a']:\$info->a) : 'test'; ?> +a)) ? (is_array(\$info)?\$info['a']:\$info->a) : 'test'; ?> EOF; $template->parse($content); @@ -326,7 +326,7 @@ EOF; ]; $data = ['name' => 'value']; $template->layout('layout')->fetch('display', $data, $config); - $this->expectOutputString('value'); + $this->expectOutputString('
value
'); } public function testDisplay() @@ -407,7 +407,7 @@ EOF; public function testIsCache() { $template = new Template(['cache_id' => '__CACHE_ID__', 'display_cache' => true]); - $this->assertTrue(!$template->isCache('__CACHE_ID__')); + $this->assertTrue($template->isCache('__CACHE_ID__')); $template->display_cache = false; $this->assertTrue(!$template->isCache('__CACHE_ID__')); }