修正标签直接使用表达式时字符串被多截取一位的bug

添加switch和case支持直接使用表达式
This commit is contained in:
oldrind
2016-01-15 16:56:51 +08:00
parent df8e3d6d61
commit 96af142bdc
3 changed files with 24 additions and 24 deletions

View File

@@ -321,11 +321,10 @@ class TagLib
if (!empty($this->tags[$tag]['expression'])) {
static $_taglibs;
if (!isset($_taglibs[$tag])) {
$_taglibs[$tag][0] = strlen($this->tpl->config('taglib_begin') . $tag);
$_taglibs[$tag][1] = strlen($this->tpl->config('taglib_end'));
$_taglibs[$tag][0] = strlen(ltrim($this->tpl->config('taglib_begin'), '\\') . $tag);
$_taglibs[$tag][1] = strlen(ltrim($this->tpl->config('taglib_end'), '\\'));
}
$str = substr($str, $_taglibs[$tag][0], -$_taglibs[$tag][1]);
$result['expression'] = trim($str);
$result['expression'] = trim(substr($str, $_taglibs[$tag][0], -$_taglibs[$tag][1]));
} elseif (empty($this->tags[$tag]) || !empty($this->tags[$tag]['attr'])) {
throw new Exception('_XML_TAG_ERROR_:' . $tag);
}

View File

@@ -30,10 +30,10 @@ class Cx extends Taglib
'volist' => ['attr' => 'name,id,offset,length,key,mod', 'alias' => 'iterate'],
'foreach' => ['attr' => 'name,id,item,key,offset,length,mod', 'expression' => true],
'if' => ['attr' => 'condition', 'expression' => true],
'elseif' => ['attr' => 'condition', 'close' => 0],
'elseif' => ['attr' => 'condition', 'close' => 0, 'expression' => true],
'else' => ['attr' => '', 'close' => 0],
'switch' => ['attr' => 'name'],
'case' => ['attr' => 'value,break'],
'switch' => ['attr' => 'name', 'expression' => true],
'case' => ['attr' => 'value,break', 'expression' => true],
'default' => ['attr' => '', 'close' => 0],
'compare' => ['attr' => 'name,value,type', 'alias' => 'eq,equal,notequal,neq,gt,lt,egt,elt,heq,nheq'],
'range' => ['attr' => 'name,value,type', 'alias' => 'in,notin,between,notbetween'],
@@ -251,7 +251,7 @@ class Cx extends Taglib
*/
public function _switch($tag, $content)
{
$name = $tag['name'];
$name = !empty($tag['expression']) ? $tag['expression'] : $tag['name'];
$name = $this->autoBuildVar($name);
$parseStr = '<?php switch(' . $name . '): ?>' . $content . '<?php endswitch;?>';
return $parseStr;
@@ -266,7 +266,7 @@ class Cx extends Taglib
*/
public function _case($tag, $content)
{
$value = $tag['value'];
$value = !empty($tag['expression']) ? $tag['expression'] : $tag['value'];
$flag = substr($value, 0, 1);
if ('$' == $flag || ':' == $flag) {
$value = $this->autoBuildVar($value);
@@ -309,6 +309,7 @@ class Cx extends Taglib
* @access public
* @param array $tag 标签属性
* @param string $content 标签内容
* @param string $type 比较类型
* @return string
*/
public function _compare($tag, $content, $type = 'eq')
@@ -319,12 +320,12 @@ class Cx extends Taglib
$name = $this->autoBuildVar($name);
$flag = substr($value, 0, 1);
if ('$' == $flag || ':' == $flag) {
$value = '(' . $this->autoBuildVar($value) . ')';
$value = $this->autoBuildVar($value);
} else {
$value = '\'' . $value . '\'';
}
$type = $this->parseCondition(' ' . $type . ' ');
$parseStr = '<?php if((' . $name . ') ' . $type . ' ' . $value . '): ?>' . $content . '<?php endif; ?>';
$parseStr = '<?php if(' . $name . ' ' . $type . ' ' . $value . '): ?>' . $content . '<?php endif; ?>';
return $parseStr;
}