[feature/template-engine] Fix negative variable expressions

compile_tag_if had the flawed approach of adding an isset statement for
all variables to the beginning of the if. This fails for negative
expressions, and checking those takes a considerable effort.

The easier solution is to make the variable expression itself
conditional, defaulting to null if it is not set.

Thanks to naderman for the solution.

PHPBB3-9726
This commit is contained in:
Igor Wiedler 2011-04-26 02:09:51 +02:00 committed by Oleg Pudeyev
parent f0b97cfdcf
commit 427a5122d5
4 changed files with 9 additions and 13 deletions

View file

@ -408,7 +408,7 @@ class phpbb_template_filter extends php_user_filter
* some adaptions for our block level methods * some adaptions for our block level methods
* @access private * @access private
*/ */
private function compile_expression($tag_args, &$vars = array()) private function compile_expression($tag_args)
{ {
$match = array(); $match = array();
preg_match_all('/(?: preg_match_all('/(?:
@ -553,14 +553,14 @@ class phpbb_template_filter extends php_user_filter
default: default:
$token = $this->generate_block_data_ref(substr($varrefs[1], 0, -1), true, $varrefs[2]) . '[\'' . $varrefs[3] . '\']'; $token = $this->generate_block_data_ref(substr($varrefs[1], 0, -1), true, $varrefs[2]) . '[\'' . $varrefs[3] . '\']';
$vars[$token] = true; $token = '(isset(' . $token . ') ? ' . $token . ' : null)';
break; break;
} }
} }
else else
{ {
$token = ($varrefs[2]) ? '$_tpldata[\'DEFINE\'][\'.\'][\'' . $varrefs[3] . '\']' : '$_rootref[\'' . $varrefs[3] . '\']'; $token = ($varrefs[2]) ? '$_tpldata[\'DEFINE\'][\'.\'][\'' . $varrefs[3] . '\']' : '$_rootref[\'' . $varrefs[3] . '\']';
$vars[$token] = true; $token = '(isset(' . $token . ') ? ' . $token . ' : null)';
} }
} }
@ -601,17 +601,10 @@ class phpbb_template_filter extends php_user_filter
private function compile_tag_if($tag_args, $elseif) private function compile_tag_if($tag_args, $elseif)
{ {
$vars = array(); $tokens = $this->compile_expression($tag_args);
$tokens = $this->compile_expression($tag_args, $vars);
$tpl = ($elseif) ? '} else if (' : 'if ('; $tpl = ($elseif) ? '} else if (' : 'if (';
if (!empty($vars))
{
$tpl .= '(isset(' . implode(') && isset(', array_keys($vars)) . ')) && ';
}
$tpl .= implode(' ', $tokens); $tpl .= implode(' ', $tokens);
$tpl .= ') { '; $tpl .= ') { ';

View file

@ -91,7 +91,7 @@ class phpbb_template_template_test extends phpbb_test_case
array(), array(),
array(), array(),
array(), array(),
"pass\npass\n<!-- DUMMY var -->", "pass\npass\npass\n<!-- DUMMY var -->",
), ),
array( array(
'variable.html', 'variable.html',

View file

@ -16,5 +16,8 @@ fail
<!-- BEGINELSE --> <!-- BEGINELSE -->
pass pass
<!-- END empty --> <!-- END empty -->
<!-- IF not S_EMPTY -->
pass
<!-- ENDIF -->
<!-- DUMMY var --> <!-- DUMMY var -->

View file

@ -6,6 +6,6 @@
03 03
<!-- ENDIF --> <!-- ENDIF -->
<!-- IF (S_VALUE > S_OTHER_VALUE) --> <!-- IF S_VALUE and S_OTHER_VALUE and (S_VALUE > S_OTHER_VALUE) -->
04 04
<!-- ENDIF --> <!-- ENDIF -->