mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 07:32:48 +08:00
Merge pull request #168 from oldrind/template
修正block标签嵌套不能解析的问题,改进对未定义变量输出的判断
This commit is contained in:
@@ -556,11 +556,17 @@ class Template
|
|||||||
$children[$parent][] = $name;
|
$children[$parent][] = $name;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
} elseif (!empty($val['parent'])) {
|
||||||
|
// 如果子标签没有被继承则用原值
|
||||||
|
$children[$val['parent']][] = $name;
|
||||||
|
$blocks[$name] = $val;
|
||||||
}
|
}
|
||||||
// 替换模板中的block标签
|
if (!$val['parent']) {
|
||||||
|
// 替换模板中的顶级block标签
|
||||||
$extend = str_replace($val['begin'] . $val['content'] . $val['end'], $replace, $extend);
|
$extend = str_replace($val['begin'] . $val['content'] . $val['end'], $replace, $extend);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
$content = $extend;
|
$content = $extend;
|
||||||
unset($blocks, $baseBlocks);
|
unset($blocks, $baseBlocks);
|
||||||
}
|
}
|
||||||
@@ -735,20 +741,15 @@ class Template
|
|||||||
$str = trim(substr($str, $pos + 1));
|
$str = trim(substr($str, $pos + 1));
|
||||||
$this->parseVar($str);
|
$this->parseVar($str);
|
||||||
$first = substr($str, 0, 1);
|
$first = substr($str, 0, 1);
|
||||||
|
if (strpos($name, ')')) {
|
||||||
|
// $name为对象或是自动识别,或者含有函数
|
||||||
if (isset($array[1])) {
|
if (isset($array[1])) {
|
||||||
$this->parseVar($array[2]);
|
$this->parseVar($array[2]);
|
||||||
$name .= $array[1] . $array[2];
|
$name .= $array[1] . $array[2];
|
||||||
if ('=' == $first) {
|
|
||||||
// {$varname?='xxx'} $varname为真时才输出xxx
|
|
||||||
$str = '<?php if(' . $name . ') echo ' . substr($str, 1) . '; ?>';
|
|
||||||
} else {
|
|
||||||
$str = '<?php echo (' . $name . ')?' . $str . '; ?>';
|
|
||||||
}
|
}
|
||||||
} elseif (')' == substr($name, -1, 1)) {
|
|
||||||
// $name为对象或是自动识别,或者含有函数
|
|
||||||
switch ($first) {
|
switch ($first) {
|
||||||
case '?':
|
case '?':
|
||||||
$str = '<?php echo ' . $name . ' ? ' . $name . ' : ' . substr($str, 1) . '; ?>';
|
$str = '<?php echo (' . $name . ') ? ' . $name . ' : ' . substr($str, 1) . '; ?>';
|
||||||
break;
|
break;
|
||||||
case '=':
|
case '=':
|
||||||
$str = '<?php if(' . $name . ') echo ' . substr($str, 1) . '; ?>';
|
$str = '<?php if(' . $name . ') echo ' . substr($str, 1) . '; ?>';
|
||||||
@@ -757,26 +758,32 @@ class Template
|
|||||||
$str = '<?php echo ' . $name . '?' . $str . '; ?>';
|
$str = '<?php echo ' . $name . '?' . $str . '; ?>';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if (isset($array[1])) {
|
||||||
|
$this->parseVar($array[2]);
|
||||||
|
$_name = ' && ' . $name . $array[1] . $array[2];
|
||||||
|
} else {
|
||||||
|
$_name = '';
|
||||||
|
}
|
||||||
// $name为数组
|
// $name为数组
|
||||||
switch ($first) {
|
switch ($first) {
|
||||||
case '?':
|
case '?':
|
||||||
// {$varname??'xxx'} $varname有定义则输出$varname,否则输出xxx
|
// {$varname??'xxx'} $varname有定义则输出$varname,否则输出xxx
|
||||||
$str = '<?php echo isset(' . $name . ') ? ' . $name . ' : ' . substr($str, 1) . '; ?>';
|
$str = '<?php echo isset(' . $name . ')' . $_name . ' ? ' . $name . ' : ' . substr($str, 1) . '; ?>';
|
||||||
break;
|
break;
|
||||||
case '=':
|
case '=':
|
||||||
// {$varname?='xxx'} $varname为真时才输出xxx
|
// {$varname?='xxx'} $varname为真时才输出xxx
|
||||||
$str = '<?php if(!empty(' . $name . ')) echo ' . substr($str, 1) . '; ?>';
|
$str = '<?php if(!empty(' . $name . ')' . $_name . ') echo ' . substr($str, 1) . '; ?>';
|
||||||
break;
|
break;
|
||||||
case ':':
|
case ':':
|
||||||
// {$varname?:'xxx'} $varname为真时输出$varname,否则输出xxx
|
// {$varname?:'xxx'} $varname为真时输出$varname,否则输出xxx
|
||||||
$str = '<?php echo !empty(' . $name . ')?' . $name . $str . '; ?>';
|
$str = '<?php echo !empty(' . $name . ')' . $_name . '?' . $name . $str . '; ?>';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (strpos($str, ':')) {
|
if (strpos($str, ':')) {
|
||||||
// {$varname ? 'a' : 'b'} $varname为真时输出a,否则输出b
|
// {$varname ? 'a' : 'b'} $varname为真时输出a,否则输出b
|
||||||
$str = '<?php echo !empty(' . $name . ')?' . $str . '; ?>';
|
$str = '<?php echo !empty(' . $name . ')' . $_name . '?' . $str . '; ?>';
|
||||||
} else {
|
} else {
|
||||||
$str = '<?php echo ' . $name . '?' . $str . '; ?>';
|
$str = '<?php echo ' . $_name . '?' . $str . '; ?>';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ EOF;
|
|||||||
{\$name.a==\$name.b?='test'}
|
{\$name.a==\$name.b?='test'}
|
||||||
EOF;
|
EOF;
|
||||||
$data = <<<EOF
|
$data = <<<EOF
|
||||||
<?php if(\$name['a']==\$name['b']) echo 'test'; ?>
|
<?php if(!empty(\$name['a']) && \$name['a']==\$name['b']) echo 'test'; ?>
|
||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
@@ -88,7 +88,7 @@ EOF;
|
|||||||
{\$name.a==\$name.b?'a':'b'}
|
{\$name.a==\$name.b?'a':'b'}
|
||||||
EOF;
|
EOF;
|
||||||
$data = <<<EOF
|
$data = <<<EOF
|
||||||
<?php echo (\$name['a']==\$name['b'])?'a':'b'; ?>
|
<?php echo !empty(\$name['a']) && \$name['a']==\$name['b']?'a':'b'; ?>
|
||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
@@ -98,7 +98,7 @@ EOF;
|
|||||||
{\$name.a|default='test'==\$name.b?'a':'b'}
|
{\$name.a|default='test'==\$name.b?'a':'b'}
|
||||||
EOF;
|
EOF;
|
||||||
$data = <<<EOF
|
$data = <<<EOF
|
||||||
<?php echo ((isset(\$name['a']) && (\$name['a'] !== '')?\$name['a']:'test')==\$name['b'])?'a':'b'; ?>
|
<?php echo (isset(\$name['a']) && (\$name['a'] !== '')?\$name['a']:'test')==\$name['b']?'a':'b'; ?>
|
||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
@@ -210,7 +210,7 @@ EOF;
|
|||||||
<#\$info.a??'test'#>
|
<#\$info.a??'test'#>
|
||||||
EOF;
|
EOF;
|
||||||
$data = <<<EOF
|
$data = <<<EOF
|
||||||
<?php echo (is_array(\$info)?\$info['a']:\$info->a) ? (is_array(\$info)?\$info['a']:\$info->a) : 'test'; ?>
|
<?php echo ((is_array(\$info)?\$info['a']:\$info->a)) ? (is_array(\$info)?\$info['a']:\$info->a) : 'test'; ?>
|
||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
|
|||||||
Reference in New Issue
Block a user