diff --git a/library/think/Template.php b/library/think/Template.php index 8a6da9fe..ffc263b9 100644 --- a/library/think/Template.php +++ b/library/think/Template.php @@ -593,40 +593,26 @@ class Template $flag = substr($str, 0, 1); switch ($flag) { case '$': // 解析模板变量 格式 {$varName} - $this->parseVar($str); - switch ($this->config['tpl_var_identify']) { - case 'array': - $begin = 0; - break; - case 'obj': - $begin = 1; - break; - default: - // 如果是自动识别.语法,则要查找:之后的?号 - $begin = strpos($str, ':'); - } // 是否带有?号 - if (false !== $pos = strpos($str, '?', $begin)) { + if (false !== $pos = strpos($str, '?')) { $array = preg_split('/([!=]={1,2}|(?<]={0,1})/', substr($str, 0, $pos), 2, PREG_SPLIT_DELIM_CAPTURE); - $name = trim($array[0]); + $name = $array[0]; + $this->parseVar($name); $this->parseVarFunction($name); $str = trim(substr($str, $pos + 1)); + $this->parseVar($str); $first = substr($str, 0, 1); if (isset($array[1])) { - // 设置了判断条件 - // XXX: 加入这句原本是为解决变量末声明的问题,但$name中是多个条件时会解析错误,故注释掉 - /*if (strpos($name, '[')) { - $name = 'isset(' . $name . ') && ' . $name; - }*/ - $name .= $array[1] . trim($array[2]); + $this->parseVar($array[2]); + $name .= $array[1] . $array[2]; if ('=' == $first) { // {$varname?='xxx'} $varname为真时才输出xxx - $str = ''; + $str = ''; } else { - $str = ''; + $str = ''; } - } elseif ($begin || ')' == substr($name, -1, 1)) { + } elseif (')' == substr($name, -1, 1)) { // $name为对象或是自动识别,或者含有函数 switch ($first) { case '?': @@ -651,18 +637,19 @@ class Template break; case ':': // {$varname?:'xxx'} $varname为真时输出$varname,否则输出xxx - $str = ''; + $str = ''; break; default: if (strpos($str, ':')) { // {$varname ? 'a' : 'b'} $varname为真时输出a,否则输出b - $str = ''; + $str = ''; } else { $str = ''; } } } } else { + $this->parseVar($str); $this->parseVarFunction($str); $str = ''; } @@ -674,11 +661,14 @@ class Template break; case '~': // 执行某个函数 $str = substr($str, 1); + $this->parseVar($str); $str = ''; break; case '-': case '+': // 输出计算 - $str = ''; + $str = substr($str, 1); + $this->parseVar($str); + $str = ''; break; case '/': // 注释标签 $flag2 = substr($str, 1, 1); @@ -777,9 +767,9 @@ class Template switch ($fun) { case 'default': // 特殊模板函数 if (false === strpos($name, '(')) { - $name = '((isset(' . $name . ') && (' . $name . ' !== \'\')?(' . $name . '):' . $args[1] . ')'; + $name = '(isset(' . $name . ') && (' . $name . ' !== \'\')?' . $name . ':' . $args[1] . ')'; } else { - $name = '((' . $name . ' !== \'\')?(' . $name . '):' . $args[1] . ')'; + $name = '(' . $name . ' !== \'\'?' . $name . ':' . $args[1] . ')'; } break; default: // 通用模板函数 @@ -981,9 +971,9 @@ class Template $begin = $this->config['tpl_begin']; $end = $this->config['tpl_end']; if (strlen(ltrim($begin, '\\')) == 1 && strlen(ltrim($end, '\\')) == 1) { - $regex = $begin . '((?:[\$\:\-\+][a-wA-w_][\w\.\:\[\(\*\/\-\+\%_]|\/[\*\/])(?>[^' . $end . ']*))' . $end; + $regex = $begin . '((?:[\$\:\-\+~][\$a-wA-w_][\w\.\:\[\(\*\/\-\+\%_]|\/[\*\/])(?>[^' . $end . ']*))' . $end; } else { - $regex = $begin . '((?:[\$\:\-\+][a-wA-w_][\w\.\:\[\(\*\/\-\+\%_]|\/[\*\/])(?>(?:(?!' . $end . ').)*))' . $end; + $regex = $begin . '((?:[\$\:\-\+~][\$a-wA-w_][\w\.\:\[\(\*\/\-\+\%_]|\/[\*\/])(?>(?:(?!' . $end . ').)*))' . $end; } break; } diff --git a/library/think/template/TagLib.php b/library/think/template/TagLib.php index 58d2b9b9..be02d6a0 100644 --- a/library/think/template/TagLib.php +++ b/library/think/template/TagLib.php @@ -324,7 +324,10 @@ class TagLib $_taglibs[$tag][0] = strlen(ltrim($this->tpl->config('taglib_begin'), '\\') . $tag); $_taglibs[$tag][1] = strlen(ltrim($this->tpl->config('taglib_end'), '\\')); } - $result['expression'] = trim(substr($str, $_taglibs[$tag][0], -$_taglibs[$tag][1])); + $result['expression'] = substr($str, $_taglibs[$tag][0], -$_taglibs[$tag][1]); + // 清除自闭合标签尾部/ + $result['expression'] = rtrim($result['expression'], '/'); + $result['expression'] = trim($result['expression']); } elseif (empty($this->tags[$tag]) || !empty($this->tags[$tag]['attr'])) { throw new Exception('_XML_TAG_ERROR_:' . $tag); } diff --git a/library/think/template/taglib/Cx.php b/library/think/template/taglib/Cx.php index 377cb0b6..27755697 100644 --- a/library/think/template/taglib/Cx.php +++ b/library/think/template/taglib/Cx.php @@ -253,7 +253,7 @@ class Cx extends Taglib { $name = !empty($tag['expression']) ? $tag['expression'] : $tag['name']; $name = $this->autoBuildVar($name); - $parseStr = '' . $content . ''; + $parseStr = '' . $content . ''; return $parseStr; } @@ -270,20 +270,20 @@ class Cx extends Taglib $flag = substr($value, 0, 1); if ('$' == $flag || ':' == $flag) { $value = $this->autoBuildVar($value); - $value = 'case ' . $value . ': '; + $value = 'case ' . $value . ':'; } elseif (strpos($value, '|')) { $values = explode('|', $value); $value = ''; foreach ($values as $val) { - $value .= 'case "' . addslashes($val) . '": '; + $value .= 'case "' . addslashes($val) . '":'; } } else { - $value = 'case "' . $value . '": '; + $value = 'case "' . $value . '":'; } $parseStr = '' . $content; $isBreak = isset($tag['break']) ? $tag['break'] : ''; if ('' == $isBreak || $isBreak) { - $parseStr .= ''; + $parseStr .= ''; } return $parseStr; }