diff --git a/library/think/Template.php b/library/think/Template.php index e2f742bf..c2cea9e1 100644 --- a/library/think/Template.php +++ b/library/think/Template.php @@ -22,7 +22,7 @@ class Template protected $data = []; // 引擎配置 protected $config = [ - 'tpl_path' => VIEW_PATH, // 模板路径 + 'tpl_path' => '', // 模板路径 'tpl_suffix' => '.html', // 默认模板文件后缀 'cache_suffix' => '.php', // 默认模板缓存后缀 'tpl_deny_func_list' => 'echo,exit', // 模板引擎禁用函数 @@ -62,6 +62,9 @@ class Template $this->config['taglib_end'] = $this->stripPreg($this->config['taglib_end']); $this->config['tpl_begin'] = $this->stripPreg($this->config['tpl_begin']); $this->config['tpl_end'] = $this->stripPreg($this->config['tpl_end']); + if (empty($this->config['tpl_path']) && defined('VIEW_PATH')) { + $this->config['tpl_path'] = VIEW_PATH; + } // 初始化模板编译存储器 $type = $this->config['compile_type'] ? $this->config['compile_type'] : 'File'; @@ -382,7 +385,8 @@ class Template $parseStr = $this->parseTemplateName($file); // 替换变量 foreach ($array as $k => $v) { - if (0 == strpos($v, '$')) { + // 以$开头字符串转换成模板变量 + if (0 === strpos($v, '$')) { $v = ltrim($this->config['tpl_begin'], '\\') . $v . ltrim($this->config['tpl_end'], '\\'); } $parseStr = str_replace('[' . $k . ']', $v, $parseStr); @@ -488,7 +492,7 @@ class Template $regex = $this->getRegex('block'); $array = []; if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) { - $right = []; + $right = $keys = []; foreach ($matches as $match) { if (empty($match['name'][0])) { if (!empty($right)) { @@ -501,6 +505,7 @@ class Template 'content' => substr($content, $start, $len), 'end' => $end, ]; + $keys[] = $begin['offset']; } else { continue; } @@ -513,6 +518,8 @@ class Template } } unset($right, $matches); + // 按blocks标签在模板中的位置排序 + array_multisort($keys, $array); } return $array; } @@ -670,9 +677,8 @@ class Template break; case '-': case '+': // 输出计算 - $str = substr($str, 1); $this->parseVar($str); - $str = ''; + $str = ''; break; case '/': // 注释标签 $flag2 = substr($str, 1, 1); diff --git a/tests/thinkphp/library/think/extend.html b/tests/thinkphp/library/think/extend.html index 194fbbf9..9aa9a58b 100644 --- a/tests/thinkphp/library/think/extend.html +++ b/tests/thinkphp/library/think/extend.html @@ -1,11 +1,4 @@ -{layout name="layout" replace="[__REPLACE__]" /} -
- {include file="include" name="$info.name" value="$info.value" /} -{block name="main"} - main -{/block} -{literal} - {$name} -{/literal} - -
\ No newline at end of file +{extend name="extend2" /} +{block name="mainbody"} + mainbody +{/block} \ No newline at end of file diff --git a/tests/thinkphp/library/think/extend2.html b/tests/thinkphp/library/think/extend2.html new file mode 100644 index 00000000..b40e9c7a --- /dev/null +++ b/tests/thinkphp/library/think/extend2.html @@ -0,0 +1,16 @@ +{layout name="layout" replace="[__REPLACE__]" /} +
+ {include file="include" name="info" value="$info.value" /} +{block name="main"} +{block name="side"} + side +{/block} +{block name="mainbody"} + +{/block} +{/block} +{literal} + {$name} +{/literal} + +
\ No newline at end of file diff --git a/tests/thinkphp/library/think/templateTest.php b/tests/thinkphp/library/think/templateTest.php index 66c741db..f01e6e4f 100644 --- a/tests/thinkphp/library/think/templateTest.php +++ b/tests/thinkphp/library/think/templateTest.php @@ -255,7 +255,7 @@ EOF; $content = <<
- + + {\$message} + mainbody + + + {\$name} @@ -288,7 +293,7 @@ EOF; $_SERVER['SERVER_NAME'] = 'server_name'; $_GET['action'] = 'action'; $_POST['action'] = 'action'; - \think\Cookie::set('action', ['name' => 'name']); + $_COOKIE['name'] = 'name'; \think\Session::set('action', ['name' => 'name']); define('SITE_NAME', 'site_name');