修正block标签嵌套时外层不能正确替换的问题;

This commit is contained in:
oldrind
2016-02-01 15:46:22 +08:00
parent d381c51892
commit fecb43aa5a
4 changed files with 39 additions and 19 deletions

View File

@@ -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 = '<?php echo ' . $flag . $str . '; ?>';
$str = '<?php echo ' . $str . '; ?>';
break;
case '/': // 注释标签
$flag2 = substr($str, 1, 1);

View File

@@ -1,11 +1,4 @@
{layout name="layout" replace="[__REPLACE__]" /}
<div>
{include file="include" name="$info.name" value="$info.value" /}
{block name="main"}
main
{/block}
{literal}
{$name}
{/literal}
<?php echo 'php code'; ?>
</div>
{extend name="extend2" /}
{block name="mainbody"}
mainbody
{/block}

View 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>

View File

@@ -255,7 +255,7 @@ EOF;
$content = <<<EOF
{extend name="extend" /}
{block name="main"}
{block name="side"}
{include file="include" name="\$user.name" value="\$user.account" /}
{\$message}{literal}{\$message}{/literal}
{/block}
@@ -263,12 +263,17 @@ EOF;
$data = <<<EOF
<nav>
<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']; ?>">
<?php echo \$message; ?>{\$message}
mainbody
{\$name}
<?php echo 'php code'; ?>
@@ -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');