Skip to content

Commit

Permalink
Fixed that scoped variables would overwrite parent scope.
Browse files Browse the repository at this point in the history
Fixes #952
  • Loading branch information
wisskid committed Mar 15, 2024
1 parent 293bc20 commit 776f3d0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/952.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixed that scoped variables would overwrite parent scope [#952](https://github.com/smarty-php/smarty/issues/952)
8 changes: 5 additions & 3 deletions src/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
);
}

}

0 comments on commit 776f3d0

Please sign in to comment.