Fixed that scoped variables would overwrite parent scope.

Fixes #952
This commit is contained in:
Simon Wisselink
2024-03-15 13:15:59 +01:00
parent 293bc20db0
commit 776f3d0042
3 changed files with 15 additions and 3 deletions

1
changelog/952.md Normal file
View File

@@ -0,0 +1 @@
- Fixed that scoped variables would overwrite parent scope [#952](https://github.com/smarty-php/smarty/issues/952)

View File

@@ -127,13 +127,15 @@ class Data
case self::SCOPE_LOCAL: case self::SCOPE_LOCAL:
default: default:
if (isset($this->tpl_vars[$tpl_var])) { 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) { if ($nocache) {
$this->tpl_vars[$tpl_var]->setNocache(true); $newVariable->setNocache(true);
} }
} else { } else {
$this->tpl_vars[$tpl_var] = new Variable($value, $nocache); $newVariable = new Variable($value, $nocache);
} }
$this->tpl_vars[$tpl_var] = $newVariable;
} }
return $this; return $this;

View File

@@ -325,4 +325,13 @@ class ScopeTest extends PHPUnit_Smarty
$this->smarty->assign('scope', 'none'); $this->smarty->assign('scope', 'none');
$r = $this->smarty->fetch('test_function_scope.tpl'); $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}')
);
}
} }