From 424dca6d24f98e72db0e57369c4739e97b2ecaef Mon Sep 17 00:00:00 2001 From: uwetews Date: Thu, 15 Sep 2016 02:56:34 +0200 Subject: [PATCH] - bugfix assigning a variable in if condition by function like {if $value = array_shift($array)} the function got called twice https://github.com/smarty-php/smarty/issues/291 --- change_log.txt | 3 ++ libs/Smarty.class.php | 2 +- .../sysplugins/smarty_internal_compile_if.php | 37 +++++++++++-------- .../smarty_internal_compile_while.php | 10 ++--- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/change_log.txt b/change_log.txt index f9e07ccb..c60f1d52 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,4 +1,7 @@ ===== 3.1.31-dev ===== (xx.xx.xx) + 15.09.2016 + - bugfix assigning a variable in if condition by function like {if $value = array_shift($array)} the function got called twice https://github.com/smarty-php/smarty/issues/291 + 11.09.2016 - improvement {math} misleading E_USER_WARNING messages when parameter value = null https://github.com/smarty-php/smarty/issues/288 - improvement move often used code snippets into methods diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 4d3dce71..6816b7dc 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -114,7 +114,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.31-dev/18'; + const SMARTY_VERSION = '3.1.31-dev/19'; /** * define variable scopes diff --git a/libs/sysplugins/smarty_internal_compile_if.php b/libs/sysplugins/smarty_internal_compile_if.php index f50750ae..84c39df0 100644 --- a/libs/sysplugins/smarty_internal_compile_if.php +++ b/libs/sysplugins/smarty_internal_compile_if.php @@ -39,13 +39,13 @@ class Smarty_Internal_Compile_If extends Smarty_Internal_CompileBase } if (is_array($parameter[ 'if condition' ])) { + if (is_array($parameter[ 'if condition' ][ 'var' ])) { + $var = $parameter[ 'if condition' ][ 'var' ][ 'var' ]; + } else { + $var = $parameter[ 'if condition' ][ 'var' ]; + } if ($compiler->nocache) { // create nocache var to make it know for further compiling - if (is_array($parameter[ 'if condition' ][ 'var' ])) { - $var = $parameter[ 'if condition' ][ 'var' ][ 'var' ]; - } else { - $var = $parameter[ 'if condition' ][ 'var' ]; - } $compiler->setNocacheInVariable($var); } $assignCompiler = new Smarty_Internal_Compile_Assign(); @@ -59,7 +59,9 @@ class Smarty_Internal_Compile_If extends Smarty_Internal_CompileBase $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ]; $_output = $assignCompiler->compile($assignAttr, $compiler, array()); } - $_output .= ""; + $_output .= "tpl_vars[{$var}]->value" . + (isset($parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ]) ? + $parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ] : '') . ") {?>"; return $_output; } else { return ""; @@ -123,15 +125,16 @@ class Smarty_Internal_Compile_Elseif extends Smarty_Internal_CompileBase } $assignCode = ''; + $var = ''; if (is_array($parameter[ 'if condition' ])) { $condition_by_assign = true; + if (is_array($parameter[ 'if condition' ][ 'var' ])) { + $var = $parameter[ 'if condition' ][ 'var' ][ 'var' ]; + } else { + $var = $parameter[ 'if condition' ][ 'var' ]; + } if ($compiler->nocache) { // create nocache var to make it know for further compiling - if (is_array($parameter[ 'if condition' ][ 'var' ])) { - $var = $parameter[ 'if condition' ][ 'var' ][ 'var' ]; - } else { - $var = $parameter[ 'if condition' ][ 'var' ]; - } $compiler->setNocacheInVariable($var); } $assignCompiler = new Smarty_Internal_Compile_Assign(); @@ -154,8 +157,10 @@ class Smarty_Internal_Compile_Elseif extends Smarty_Internal_CompileBase if ($condition_by_assign) { $this->openTag($compiler, 'elseif', array($nesting + 1, $compiler->tag_nocache)); $_output = $compiler->appendCode("", $assignCode); - return $compiler->appendCode($_output, - ""); + return $compiler->appendCode($_output, "tpl_vars[{$var}]->value" . + (isset($parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ]) ? + $parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ] : + '') . ") {?>"); } else { $this->openTag($compiler, 'elseif', array($nesting, $compiler->tag_nocache)); return ""; @@ -165,8 +170,10 @@ class Smarty_Internal_Compile_Elseif extends Smarty_Internal_CompileBase $this->openTag($compiler, 'elseif', array($nesting + 1, $compiler->tag_nocache)); if ($condition_by_assign) { $_output = $compiler->appendCode($_output, $assignCode); - return $compiler->appendCode($_output, - ""); + return $compiler->appendCode($_output, "tpl_vars[{$var}]->value" . + (isset($parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ]) ? + $parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ] : + '') . ") {?>"); } else { return $compiler->appendCode($_output, ""); } diff --git a/libs/sysplugins/smarty_internal_compile_while.php b/libs/sysplugins/smarty_internal_compile_while.php index 30d83095..6025798c 100644 --- a/libs/sysplugins/smarty_internal_compile_while.php +++ b/libs/sysplugins/smarty_internal_compile_while.php @@ -41,7 +41,6 @@ class Smarty_Internal_Compile_While extends Smarty_Internal_CompileBase $compiler->nocache = $compiler->nocache | $compiler->tag_nocache; if (is_array($parameter[ 'if condition' ])) { if ($compiler->nocache) { - $_nocache = ',true'; // create nocache var to make it know for further compiling if (is_array($parameter[ 'if condition' ][ 'var' ])) { $var = $parameter[ 'if condition' ][ 'var' ][ 'var' ]; @@ -49,20 +48,19 @@ class Smarty_Internal_Compile_While extends Smarty_Internal_CompileBase $var = $parameter[ 'if condition' ][ 'var' ]; } $compiler->setNocacheInVariable($var); - } else { - $_nocache = ''; } + $prefixVar = $compiler->getNewPrefixVariable(); $assignCompiler = new Smarty_Internal_Compile_Assign(); $assignAttr = array(); - $assignAttr[][ 'value' ] = $parameter[ 'if condition' ][ 'value' ]; + $assignAttr[][ 'value' ] = "{$prefixVar}"; if (is_array($parameter[ 'if condition' ][ 'var' ])) { $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ][ 'var' ]; - $_output = ""; + $_output = ""; $_output .= $assignCompiler->compile($assignAttr, $compiler, array('smarty_internal_index' => $parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ])); } else { $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ]; - $_output = ""; + $_output = ""; $_output .= $assignCompiler->compile($assignAttr, $compiler, array()); }