From cd28347130a1cf0b9ee1848fed0b57c1b2f8d285 Mon Sep 17 00:00:00 2001 From: uwetews Date: Thu, 15 Sep 2016 08:36:12 +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 --- libs/Smarty.class.php | 2 +- .../sysplugins/smarty_internal_compile_if.php | 32 ++++++++----------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 867979de..f9fd353c 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/21'; + const SMARTY_VERSION = '3.1.31-dev/22'; /** * define variable scopes diff --git a/libs/sysplugins/smarty_internal_compile_if.php b/libs/sysplugins/smarty_internal_compile_if.php index 84c39df0..14db55d4 100644 --- a/libs/sysplugins/smarty_internal_compile_if.php +++ b/libs/sysplugins/smarty_internal_compile_if.php @@ -48,20 +48,20 @@ class Smarty_Internal_Compile_If extends Smarty_Internal_CompileBase // create nocache var to make it know for further compiling $compiler->setNocacheInVariable($var); } - $assignCompiler = new Smarty_Internal_Compile_Assign(); + $prefixVar = $compiler->getNewPrefixVariable(); + $_output = "\n"; $assignAttr = array(); - $assignAttr[][ 'value' ] = $parameter[ 'if condition' ][ 'value' ]; + $assignAttr[][ 'value' ] = "{$prefixVar}"; + $assignCompiler = new Smarty_Internal_Compile_Assign(); if (is_array($parameter[ 'if condition' ][ 'var' ])) { $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ][ 'var' ]; - $_output = $assignCompiler->compile($assignAttr, $compiler, + $_output .= $assignCompiler->compile($assignAttr, $compiler, array('smarty_internal_index' => $parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ])); } else { $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ]; - $_output = $assignCompiler->compile($assignAttr, $compiler, array()); + $_output .= $assignCompiler->compile($assignAttr, $compiler, array()); } - $_output .= "tpl_vars[{$var}]->value" . - (isset($parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ]) ? - $parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ] : '') . ") {?>"; + $_output .= ""; return $_output; } else { return ""; @@ -137,16 +137,18 @@ class Smarty_Internal_Compile_Elseif extends Smarty_Internal_CompileBase // create nocache var to make it know for further compiling $compiler->setNocacheInVariable($var); } + $prefixVar = $compiler->getNewPrefixVariable(); + $assignCode = "\n"; $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' ]; - $assignCode = $assignCompiler->compile($assignAttr, $compiler, + $assignCode .= $assignCompiler->compile($assignAttr, $compiler, array('smarty_internal_index' => $parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ])); } else { $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ]; - $assignCode = $assignCompiler->compile($assignAttr, $compiler, array()); + $assignCode .= $assignCompiler->compile($assignAttr, $compiler, array()); } } else { $condition_by_assign = false; @@ -157,10 +159,7 @@ 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, "tpl_vars[{$var}]->value" . - (isset($parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ]) ? - $parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ] : - '') . ") {?>"); + return $compiler->appendCode($_output, ""); } else { $this->openTag($compiler, 'elseif', array($nesting, $compiler->tag_nocache)); return ""; @@ -170,10 +169,7 @@ 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, "tpl_vars[{$var}]->value" . - (isset($parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ]) ? - $parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ] : - '') . ") {?>"); + return $compiler->appendCode($_output, ""); } else { return $compiler->appendCode($_output, ""); }