mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 15:12:48 +08:00
Merge pull request #243 from oldrind/template
提升template类测试覆盖率,修正include标签传变量
This commit is contained in:
@@ -383,6 +383,9 @@ class Template
|
|||||||
$parseStr = $this->parseTemplateName($file);
|
$parseStr = $this->parseTemplateName($file);
|
||||||
// 替换变量
|
// 替换变量
|
||||||
foreach ($array as $k => $v) {
|
foreach ($array as $k => $v) {
|
||||||
|
if (0 == strpos($v, '$')) {
|
||||||
|
$v = ltrim($this->config['tpl_begin'], '\\') . $v . ltrim($this->config['tpl_end'], '\\');
|
||||||
|
}
|
||||||
$parseStr = str_replace('[' . $k . ']', $v, $parseStr);
|
$parseStr = str_replace('[' . $k . ']', $v, $parseStr);
|
||||||
}
|
}
|
||||||
// 再次对包含文件进行模板分析
|
// 再次对包含文件进行模板分析
|
||||||
@@ -455,10 +458,12 @@ class Template
|
|||||||
$regex = $this->getRegex($restore ? 'restoreliteral' : 'literal');
|
$regex = $this->getRegex($restore ? 'restoreliteral' : 'literal');
|
||||||
if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER)) {
|
if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER)) {
|
||||||
if (!$restore) {
|
if (!$restore) {
|
||||||
|
$count = count($this->literal);
|
||||||
// 替换literal标签
|
// 替换literal标签
|
||||||
foreach ($matches as $i => $match) {
|
foreach ($matches as $i => $match) {
|
||||||
$this->literal[] = substr($match[0], strlen($match[1]), -strlen($match[2]));
|
$this->literal[] = substr($match[0], strlen($match[1]), -strlen($match[2]));
|
||||||
$content = str_replace($match[0], "<!--###literal{$i}###-->", $content);
|
$content = str_replace($match[0], "<!--###literal{$count}###-->", $content);
|
||||||
|
$count++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 还原literal标签
|
// 还原literal标签
|
||||||
@@ -821,14 +826,14 @@ class Template
|
|||||||
if (isset($vars[2])) {
|
if (isset($vars[2])) {
|
||||||
$parseStr = '$_COOKIE[\'' . $vars[1] . '\'][\'' . $vars[2] . '\']';
|
$parseStr = '$_COOKIE[\'' . $vars[1] . '\'][\'' . $vars[2] . '\']';
|
||||||
} else {
|
} else {
|
||||||
$parseStr = '\\think\\cookie::get(\'' . $vars[1] . '\')';
|
$parseStr = '\\think\\Cookie::get(\'' . $vars[1] . '\')';
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'SESSION':
|
case 'SESSION':
|
||||||
if (isset($vars[2])) {
|
if (isset($vars[2])) {
|
||||||
$parseStr = '$_SESSION[\'' . $vars[1] . '\'][\'' . $vars[2] . '\']';
|
$parseStr = '$_SESSION[\'' . $vars[1] . '\'][\'' . $vars[2] . '\']';
|
||||||
} else {
|
} else {
|
||||||
$parseStr = '\\think\\session::get(\'' . $vars[1] . '\')';
|
$parseStr = '\\think\\Session::get(\'' . $vars[1] . '\')';
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'ENV':
|
case 'ENV':
|
||||||
@@ -841,13 +846,13 @@ class Template
|
|||||||
$parseStr = strtoupper($vars[1]);
|
$parseStr = strtoupper($vars[1]);
|
||||||
break;
|
break;
|
||||||
case 'LANG':
|
case 'LANG':
|
||||||
$parseStr = '\\think\\lang::get(\'' . $vars[1] . '\')';
|
$parseStr = '\\think\\Lang::get(\'' . $vars[1] . '\')';
|
||||||
break;
|
break;
|
||||||
case 'CONFIG':
|
case 'CONFIG':
|
||||||
if (isset($vars[2])) {
|
if (isset($vars[2])) {
|
||||||
$vars[1] .= '.' . $vars[2];
|
$vars[1] .= '.' . $vars[2];
|
||||||
}
|
}
|
||||||
$parseStr = '\\think\\config::get(\'' . $vars[1] . '\')';
|
$parseStr = '\\think\\Config::get(\'' . $vars[1] . '\')';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@@ -862,10 +867,10 @@ class Template
|
|||||||
$parseStr = 'THINK_VERSION';
|
$parseStr = 'THINK_VERSION';
|
||||||
break;
|
break;
|
||||||
case 'LDELIM':
|
case 'LDELIM':
|
||||||
$parseStr = $this->config['tpl_begin'];
|
$parseStr = '\'' . ltrim($this->config['tpl_begin'], '\\') . '\'';
|
||||||
break;
|
break;
|
||||||
case 'RDELIM':
|
case 'RDELIM':
|
||||||
$parseStr = $this->config['tpl_end'];
|
$parseStr = '\'' . ltrim($this->config['tpl_end'], '\\') . '\'';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (defined($vars[0])) {
|
if (defined($vars[0])) {
|
||||||
|
|||||||
@@ -136,12 +136,12 @@ class TagLib
|
|||||||
// 标签替换 从后向前
|
// 标签替换 从后向前
|
||||||
foreach ($nodes as $pos => $node) {
|
foreach ($nodes as $pos => $node) {
|
||||||
// 对应的标签名
|
// 对应的标签名
|
||||||
$name = $tags[1][$node['name']];
|
$name = $node['name'];
|
||||||
// 解析标签属性
|
// 解析标签属性
|
||||||
$attrs = $this->parseAttr($node['begin'][0], $name);
|
$attrs = $this->parseAttr($node['begin'][0], $name);
|
||||||
$method = '_' . $name;
|
$method = '_' . $name;
|
||||||
// 读取标签库中对应的标签内容 replace[0]用来替换标签头,replace[1]用来替换标签尾
|
// 读取标签库中对应的标签内容 replace[0]用来替换标签头,replace[1]用来替换标签尾
|
||||||
$replace = explode($break, $this->$method($attrs, $break, $node['name']));
|
$replace = explode($break, $this->$method($attrs, $break));
|
||||||
if (count($replace) > 1) {
|
if (count($replace) > 1) {
|
||||||
while ($beginArray) {
|
while ($beginArray) {
|
||||||
$begin = end($beginArray);
|
$begin = end($beginArray);
|
||||||
@@ -171,13 +171,13 @@ class TagLib
|
|||||||
// 自闭合标签
|
// 自闭合标签
|
||||||
if (!empty($tags[0])) {
|
if (!empty($tags[0])) {
|
||||||
$regex = $this->getRegex(array_keys($tags[0]), 0);
|
$regex = $this->getRegex(array_keys($tags[0]), 0);
|
||||||
$self = &$this;
|
$content = preg_replace_callback($regex, function ($matches) use (&$tags) {
|
||||||
$content = preg_replace_callback($regex, function ($matches) use (&$tags, &$self) {
|
// 对应的标签名
|
||||||
$name = $tags[0][$matches[1]];
|
$name = $matches[1];
|
||||||
// 解析标签属性
|
// 解析标签属性
|
||||||
$attrs = $self->parseAttr($matches[0], $name);
|
$attrs = $this->parseAttr($matches[0], $name);
|
||||||
$method = '_' . $name;
|
$method = '_' . $name;
|
||||||
return $self->$method($attrs, '', $matches[1]);
|
return $this->$method($attrs, '');
|
||||||
}, $content);
|
}, $content);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|||||||
11
tests/thinkphp/library/think/extend.html
Normal file
11
tests/thinkphp/library/think/extend.html
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
{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>
|
||||||
1
tests/thinkphp/library/think/include.html
Normal file
1
tests/thinkphp/library/think/include.html
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<input name="[name]" value="[value]">
|
||||||
2
tests/thinkphp/library/think/layout.html
Normal file
2
tests/thinkphp/library/think/layout.html
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
<nav>[__REPLACE__]
|
||||||
|
</nav>
|
||||||
@@ -32,7 +32,7 @@ EOF;
|
|||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
$this->assertEquals($content, $data);
|
$this->assertEquals($data, $content);
|
||||||
|
|
||||||
$content = <<<EOF
|
$content = <<<EOF
|
||||||
{\$name.a??'test'}
|
{\$name.a??'test'}
|
||||||
@@ -42,7 +42,7 @@ EOF;
|
|||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
$this->assertEquals($content, $data);
|
$this->assertEquals($data, $content);
|
||||||
|
|
||||||
$content = <<<EOF
|
$content = <<<EOF
|
||||||
{\$name.a?='test'}
|
{\$name.a?='test'}
|
||||||
@@ -52,7 +52,7 @@ EOF;
|
|||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
$this->assertEquals($content, $data);
|
$this->assertEquals($data, $content);
|
||||||
|
|
||||||
$content = <<<EOF
|
$content = <<<EOF
|
||||||
{\$name.a?:'test'}
|
{\$name.a?:'test'}
|
||||||
@@ -62,7 +62,7 @@ EOF;
|
|||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
$this->assertEquals($content, $data);
|
$this->assertEquals($data, $content);
|
||||||
|
|
||||||
$content = <<<EOF
|
$content = <<<EOF
|
||||||
{\$name.a?\$name.b:'no'}
|
{\$name.a?\$name.b:'no'}
|
||||||
@@ -72,7 +72,7 @@ EOF;
|
|||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
$this->assertEquals($content, $data);
|
$this->assertEquals($data, $content);
|
||||||
|
|
||||||
$content = <<<EOF
|
$content = <<<EOF
|
||||||
{\$name.a==\$name.b?='test'}
|
{\$name.a==\$name.b?='test'}
|
||||||
@@ -82,7 +82,7 @@ EOF;
|
|||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
$this->assertEquals($content, $data);
|
$this->assertEquals($data, $content);
|
||||||
|
|
||||||
$content = <<<EOF
|
$content = <<<EOF
|
||||||
{\$name.a==\$name.b?'a':'b'}
|
{\$name.a==\$name.b?'a':'b'}
|
||||||
@@ -92,7 +92,7 @@ EOF;
|
|||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
$this->assertEquals($content, $data);
|
$this->assertEquals($data, $content);
|
||||||
|
|
||||||
$content = <<<EOF
|
$content = <<<EOF
|
||||||
{\$name.a|default='test'==\$name.b?'a':'b'}
|
{\$name.a|default='test'==\$name.b?'a':'b'}
|
||||||
@@ -102,7 +102,7 @@ EOF;
|
|||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
$this->assertEquals($content, $data);
|
$this->assertEquals($data, $content);
|
||||||
|
|
||||||
$content = <<<EOF
|
$content = <<<EOF
|
||||||
{:ltrim(rtrim(\$name.a))}
|
{:ltrim(rtrim(\$name.a))}
|
||||||
@@ -112,7 +112,7 @@ EOF;
|
|||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
$this->assertEquals($content, $data);
|
$this->assertEquals($data, $content);
|
||||||
|
|
||||||
$content = <<<EOF
|
$content = <<<EOF
|
||||||
{~echo(trim(\$name.a))}
|
{~echo(trim(\$name.a))}
|
||||||
@@ -122,7 +122,7 @@ EOF;
|
|||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
$this->assertEquals($content, $data);
|
$this->assertEquals($data, $content);
|
||||||
|
|
||||||
$content = <<<EOF
|
$content = <<<EOF
|
||||||
{+\$name.a}
|
{+\$name.a}
|
||||||
@@ -132,7 +132,7 @@ EOF;
|
|||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
$this->assertEquals($content, $data);
|
$this->assertEquals($data, $content);
|
||||||
|
|
||||||
$content = <<<EOF
|
$content = <<<EOF
|
||||||
{/*\$name*/}
|
{/*\$name*/}
|
||||||
@@ -140,7 +140,7 @@ EOF;
|
|||||||
$data = '';
|
$data = '';
|
||||||
|
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
$this->assertEquals($content, $data);
|
$this->assertEquals($data, $content);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,7 +159,7 @@ EOF;
|
|||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
$this->assertEquals($content, $data);
|
$this->assertEquals($data, $content);
|
||||||
|
|
||||||
$content = <<<EOF
|
$content = <<<EOF
|
||||||
<#\$info.a==\$info.b?='test'#>
|
<#\$info.a==\$info.b?='test'#>
|
||||||
@@ -169,7 +169,7 @@ EOF;
|
|||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
$this->assertEquals($content, $data);
|
$this->assertEquals($data, $content);
|
||||||
|
|
||||||
$content = <<<EOF
|
$content = <<<EOF
|
||||||
<#\$info.a|default='test'?'yes':'no'#>
|
<#\$info.a|default='test'?'yes':'no'#>
|
||||||
@@ -179,86 +179,93 @@ EOF;
|
|||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
$this->assertEquals($content, $data);
|
$this->assertEquals($data, $content);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testTag()
|
public function testTag()
|
||||||
{
|
{
|
||||||
$template = new Template();
|
$config['tpl_path'] = dirname(__FILE__) . '/';
|
||||||
|
$config['tpl_suffix'] = '.html';
|
||||||
|
$template = new Template($config);
|
||||||
|
|
||||||
$content = <<<EOF
|
$content = <<<EOF
|
||||||
{if \$var.a==\$var.b}
|
{extend name="extend" /}
|
||||||
one
|
{block name="main"}
|
||||||
{elseif !empty(\$var.a) /}
|
{include file="include" name="\$user.name" value="\$user.account" /}
|
||||||
two
|
{\$message}{literal}{\$message}{/literal}
|
||||||
{else /}
|
{/block}
|
||||||
default
|
|
||||||
{/if}
|
|
||||||
EOF;
|
EOF;
|
||||||
$data = <<<EOF
|
$data = <<<EOF
|
||||||
<?php if(\$var['a']==\$var['b']): ?>
|
<nav>
|
||||||
one
|
<div>
|
||||||
<?php elseif(!empty(\$var['a'])): ?>
|
<input name="<?php echo \$info['name']; ?>" value="<?php echo \$info['value']; ?>">
|
||||||
two
|
|
||||||
<?php else: ?>
|
<input name="<?php echo \$user['name']; ?>" value="<?php echo \$user['account']; ?>">
|
||||||
default
|
<?php echo \$message; ?>{\$message}
|
||||||
<?php endif; ?>
|
|
||||||
|
|
||||||
|
{\$name}
|
||||||
|
|
||||||
|
<?php echo 'php code'; ?>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
EOF;
|
EOF;
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
$this->assertEquals($content, $data);
|
$this->assertEquals($data, $content);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testThinkVar()
|
||||||
|
{
|
||||||
|
$config['tpl_begin'] = '{';
|
||||||
|
$config['tpl_end'] = '}';
|
||||||
|
$template = new Template($config);
|
||||||
|
|
||||||
|
$_SERVER['SERVER_NAME'] = 'server_name';
|
||||||
|
$_GET['action'] = 'action';
|
||||||
|
$_POST['action'] = 'action';
|
||||||
|
\think\Cookie::set('action', ['name' => 'name']);
|
||||||
|
\think\Session::set('action', ['name' => 'name']);
|
||||||
|
define('SITE_NAME', 'site_name');
|
||||||
|
|
||||||
$content = <<<EOF
|
$content = <<<EOF
|
||||||
{switch \$var}
|
{\$Think.SERVER.SERVER_NAME}<br/>
|
||||||
{case \$a /}
|
{\$Think.GET.action}<br/>
|
||||||
a
|
{\$Think.POST.action}<br/>
|
||||||
{/case}
|
{\$Think.COOKIE.action}<br/>
|
||||||
{case b}
|
{\$Think.COOKIE.action.name}<br/>
|
||||||
b
|
{\$Think.SESSION.action}<br/>
|
||||||
{/case}
|
{\$Think.SESSION.action.name}<br/>
|
||||||
{default /}
|
{\$Think.ENV.OS}<br/>
|
||||||
default
|
{\$Think.REQUEST.action}<br/>
|
||||||
{/switch}
|
{\$Think.CONST.SITE_NAME}<br/>
|
||||||
|
{\$Think.LANG.action}<br/>
|
||||||
|
{\$Think.CONFIG.action.name}<br/>
|
||||||
|
{\$Think.NOW}<br/>
|
||||||
|
{\$Think.VERSION}<br/>
|
||||||
|
{\$Think.LDELIM}<br/>
|
||||||
|
{\$Think.RDELIM}<br/>
|
||||||
|
{\$Think.SITE_NAME}
|
||||||
EOF;
|
EOF;
|
||||||
$data = <<<EOF
|
$data = <<<EOF
|
||||||
<?php switch(\$var): ?>
|
<?php echo \$_SERVER['SERVER_NAME']; ?><br/>
|
||||||
<?php case \$a: ?>
|
<?php echo \$_GET['action']; ?><br/>
|
||||||
a
|
<?php echo \$_POST['action']; ?><br/>
|
||||||
<?php break; ?>
|
<?php echo \\think\\Cookie::get('action'); ?><br/>
|
||||||
<?php case "b": ?>
|
<?php echo \$_COOKIE['action']['name']; ?><br/>
|
||||||
b
|
<?php echo \\think\\Session::get('action'); ?><br/>
|
||||||
<?php break; ?>
|
<?php echo \$_SESSION['action']['name']; ?><br/>
|
||||||
<?php default: ?>
|
<?php echo \$_ENV['OS']; ?><br/>
|
||||||
default
|
<?php echo \$_REQUEST['action']; ?><br/>
|
||||||
<?php endswitch; ?>
|
<?php echo SITE_NAME; ?><br/>
|
||||||
|
<?php echo \\think\\Lang::get('action'); ?><br/>
|
||||||
|
<?php echo \\think\\Config::get('action.name'); ?><br/>
|
||||||
|
<?php echo date('Y-m-d g:i a',time()); ?><br/>
|
||||||
|
<?php echo THINK_VERSION; ?><br/>
|
||||||
|
<?php echo '{'; ?><br/>
|
||||||
|
<?php echo '}'; ?><br/>
|
||||||
|
<?php echo SITE_NAME; ?>
|
||||||
EOF;
|
EOF;
|
||||||
$template->parse($content);
|
$template->parse($content);
|
||||||
$this->assertEquals($content, $data);
|
$this->assertEquals($data, $content);
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
{foreach \$list as \$key=>\$val}
|
|
||||||
|
|
||||||
{/foreach}
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php foreach(\$list as \$key=>\$val): ?>
|
|
||||||
|
|
||||||
<?php endforeach; ?>
|
|
||||||
EOF;
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($content, $data);
|
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
{foreach name="list" id="val" key="key"}
|
|
||||||
|
|
||||||
{/foreach}
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php if(is_array(\$list)): foreach(\$list as \$key=>\$val): ?>
|
|
||||||
|
|
||||||
<?php endforeach; endif; ?>
|
|
||||||
EOF;
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($content, $data);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user