From 776f3d0042c508eb41dec586bb1433fb0f16c281 Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Fri, 15 Mar 2024 13:15:59 +0100 Subject: [PATCH] Fixed that scoped variables would overwrite parent scope. Fixes #952 --- changelog/952.md | 1 + src/Data.php | 8 +++++--- tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php | 9 +++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 changelog/952.md diff --git a/changelog/952.md b/changelog/952.md new file mode 100644 index 000000000..b53e133d9 --- /dev/null +++ b/changelog/952.md @@ -0,0 +1 @@ +- Fixed that scoped variables would overwrite parent scope [#952](https://github.com/smarty-php/smarty/issues/952) \ No newline at end of file diff --git a/src/Data.php b/src/Data.php index adacf2169..d6db3e84e 100644 --- a/src/Data.php +++ b/src/Data.php @@ -127,13 +127,15 @@ public function assign($tpl_var, $value = null, $nocache = false, $scope = null) case self::SCOPE_LOCAL: default: if (isset($this->tpl_vars[$tpl_var])) { - $this->tpl_vars[$tpl_var]->setValue($value); + $newVariable = clone $this->tpl_vars[$tpl_var]; + $newVariable->setValue($value); if ($nocache) { - $this->tpl_vars[$tpl_var]->setNocache(true); + $newVariable->setNocache(true); } } else { - $this->tpl_vars[$tpl_var] = new Variable($value, $nocache); + $newVariable = new Variable($value, $nocache); } + $this->tpl_vars[$tpl_var] = $newVariable; } return $this; diff --git a/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php b/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php index 4f805fae5..41b35f73c 100644 --- a/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php +++ b/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php @@ -325,4 +325,13 @@ public function testFunctionScope() $this->smarty->assign('scope', 'none'); $r = $this->smarty->fetch('test_function_scope.tpl'); } + + public function testFunctionScopeIsLocaLByDefault() + { + $this->assertEquals( + 'a', + $this->smarty->fetch('string:{function name=test}{$var="b"}{/function}{$var="a"}{test}{$var}') + ); + } + }