mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-05 22:52:49 +08:00
修正block标签嵌套时外层不能正确替换的问题;
This commit is contained in:
@@ -22,7 +22,7 @@ class Template
|
|||||||
protected $data = [];
|
protected $data = [];
|
||||||
// 引擎配置
|
// 引擎配置
|
||||||
protected $config = [
|
protected $config = [
|
||||||
'tpl_path' => VIEW_PATH, // 模板路径
|
'tpl_path' => '', // 模板路径
|
||||||
'tpl_suffix' => '.html', // 默认模板文件后缀
|
'tpl_suffix' => '.html', // 默认模板文件后缀
|
||||||
'cache_suffix' => '.php', // 默认模板缓存后缀
|
'cache_suffix' => '.php', // 默认模板缓存后缀
|
||||||
'tpl_deny_func_list' => 'echo,exit', // 模板引擎禁用函数
|
'tpl_deny_func_list' => 'echo,exit', // 模板引擎禁用函数
|
||||||
@@ -62,6 +62,9 @@ class Template
|
|||||||
$this->config['taglib_end'] = $this->stripPreg($this->config['taglib_end']);
|
$this->config['taglib_end'] = $this->stripPreg($this->config['taglib_end']);
|
||||||
$this->config['tpl_begin'] = $this->stripPreg($this->config['tpl_begin']);
|
$this->config['tpl_begin'] = $this->stripPreg($this->config['tpl_begin']);
|
||||||
$this->config['tpl_end'] = $this->stripPreg($this->config['tpl_end']);
|
$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';
|
$type = $this->config['compile_type'] ? $this->config['compile_type'] : 'File';
|
||||||
@@ -382,7 +385,8 @@ class Template
|
|||||||
$parseStr = $this->parseTemplateName($file);
|
$parseStr = $this->parseTemplateName($file);
|
||||||
// 替换变量
|
// 替换变量
|
||||||
foreach ($array as $k => $v) {
|
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'], '\\');
|
$v = ltrim($this->config['tpl_begin'], '\\') . $v . ltrim($this->config['tpl_end'], '\\');
|
||||||
}
|
}
|
||||||
$parseStr = str_replace('[' . $k . ']', $v, $parseStr);
|
$parseStr = str_replace('[' . $k . ']', $v, $parseStr);
|
||||||
@@ -488,7 +492,7 @@ class Template
|
|||||||
$regex = $this->getRegex('block');
|
$regex = $this->getRegex('block');
|
||||||
$array = [];
|
$array = [];
|
||||||
if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) {
|
if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) {
|
||||||
$right = [];
|
$right = $keys = [];
|
||||||
foreach ($matches as $match) {
|
foreach ($matches as $match) {
|
||||||
if (empty($match['name'][0])) {
|
if (empty($match['name'][0])) {
|
||||||
if (!empty($right)) {
|
if (!empty($right)) {
|
||||||
@@ -501,6 +505,7 @@ class Template
|
|||||||
'content' => substr($content, $start, $len),
|
'content' => substr($content, $start, $len),
|
||||||
'end' => $end,
|
'end' => $end,
|
||||||
];
|
];
|
||||||
|
$keys[] = $begin['offset'];
|
||||||
} else {
|
} else {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -513,6 +518,8 @@ class Template
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
unset($right, $matches);
|
unset($right, $matches);
|
||||||
|
// 按blocks标签在模板中的位置排序
|
||||||
|
array_multisort($keys, $array);
|
||||||
}
|
}
|
||||||
return $array;
|
return $array;
|
||||||
}
|
}
|
||||||
@@ -670,9 +677,8 @@ class Template
|
|||||||
break;
|
break;
|
||||||
case '-':
|
case '-':
|
||||||
case '+': // 输出计算
|
case '+': // 输出计算
|
||||||
$str = substr($str, 1);
|
|
||||||
$this->parseVar($str);
|
$this->parseVar($str);
|
||||||
$str = '<?php echo ' . $flag . $str . '; ?>';
|
$str = '<?php echo ' . $str . '; ?>';
|
||||||
break;
|
break;
|
||||||
case '/': // 注释标签
|
case '/': // 注释标签
|
||||||
$flag2 = substr($str, 1, 1);
|
$flag2 = substr($str, 1, 1);
|
||||||
|
|||||||
@@ -1,11 +1,4 @@
|
|||||||
{layout name="layout" replace="[__REPLACE__]" /}
|
{extend name="extend2" /}
|
||||||
<div>
|
{block name="mainbody"}
|
||||||
{include file="include" name="$info.name" value="$info.value" /}
|
mainbody
|
||||||
{block name="main"}
|
|
||||||
main
|
|
||||||
{/block}
|
{/block}
|
||||||
{literal}
|
|
||||||
{$name}
|
|
||||||
{/literal}
|
|
||||||
<?php echo 'php code'; ?>
|
|
||||||
</div>
|
|
||||||
16
tests/thinkphp/library/think/extend2.html
Normal file
16
tests/thinkphp/library/think/extend2.html
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
{layout name="layout" replace="[__REPLACE__]" /}
|
||||||
|
<div>
|
||||||
|
{include file="include" name="info" value="$info.value" /}
|
||||||
|
{block name="main"}
|
||||||
|
{block name="side"}
|
||||||
|
side
|
||||||
|
{/block}
|
||||||
|
{block name="mainbody"}
|
||||||
|
|
||||||
|
{/block}
|
||||||
|
{/block}
|
||||||
|
{literal}
|
||||||
|
{$name}
|
||||||
|
{/literal}
|
||||||
|
<?php echo 'php code'; ?>
|
||||||
|
</div>
|
||||||
@@ -255,7 +255,7 @@ EOF;
|
|||||||
|
|
||||||
$content = <<<EOF
|
$content = <<<EOF
|
||||||
{extend name="extend" /}
|
{extend name="extend" /}
|
||||||
{block name="main"}
|
{block name="side"}
|
||||||
{include file="include" name="\$user.name" value="\$user.account" /}
|
{include file="include" name="\$user.name" value="\$user.account" /}
|
||||||
{\$message}{literal}{\$message}{/literal}
|
{\$message}{literal}{\$message}{/literal}
|
||||||
{/block}
|
{/block}
|
||||||
@@ -263,12 +263,17 @@ EOF;
|
|||||||
$data = <<<EOF
|
$data = <<<EOF
|
||||||
<nav>
|
<nav>
|
||||||
<div>
|
<div>
|
||||||
<input name="<?php echo \$info['name']; ?>" value="<?php echo \$info['value']; ?>">
|
<input name="info" value="<?php echo \$info['value']; ?>">
|
||||||
|
|
||||||
|
|
||||||
<input name="<?php echo \$user['name']; ?>" value="<?php echo \$user['account']; ?>">
|
<input name="<?php echo \$user['name']; ?>" value="<?php echo \$user['account']; ?>">
|
||||||
<?php echo \$message; ?>{\$message}
|
<?php echo \$message; ?>{\$message}
|
||||||
|
|
||||||
|
|
||||||
|
mainbody
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
{\$name}
|
{\$name}
|
||||||
|
|
||||||
<?php echo 'php code'; ?>
|
<?php echo 'php code'; ?>
|
||||||
@@ -288,7 +293,7 @@ EOF;
|
|||||||
$_SERVER['SERVER_NAME'] = 'server_name';
|
$_SERVER['SERVER_NAME'] = 'server_name';
|
||||||
$_GET['action'] = 'action';
|
$_GET['action'] = 'action';
|
||||||
$_POST['action'] = 'action';
|
$_POST['action'] = 'action';
|
||||||
\think\Cookie::set('action', ['name' => 'name']);
|
$_COOKIE['name'] = 'name';
|
||||||
\think\Session::set('action', ['name' => 'name']);
|
\think\Session::set('action', ['name' => 'name']);
|
||||||
define('SITE_NAME', 'site_name');
|
define('SITE_NAME', 'site_name');
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user