From 6f054ecc2ffea472b438691f42afcad598b98d61 Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Thu, 28 Mar 2024 11:22:29 +0100 Subject: [PATCH] Fix Smarty::assign() not returning when called with an array as first parameter. (#973) Fixes #972 --- changelog/972.md | 1 + src/Data.php | 2 +- .../SmartyMethodsTests/Assign/AssignTest.php | 11 +++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 changelog/972.md diff --git a/changelog/972.md b/changelog/972.md new file mode 100644 index 000000000..e1e930a3c --- /dev/null +++ b/changelog/972.md @@ -0,0 +1 @@ +- Fix Smarty::assign() not returning $this when called with an array as first parameter [#972](https://github.com/smarty-php/smarty/pull/972) \ No newline at end of file diff --git a/src/Data.php b/src/Data.php index 3176c7f05..582ee6601 100644 --- a/src/Data.php +++ b/src/Data.php @@ -109,7 +109,7 @@ public function assign($tpl_var, $value = null, $nocache = false, $scope = null) foreach ($tpl_var as $_key => $_val) { $this->assign($_key, $_val, $nocache, $scope); } - return; + return $this; } switch ($scope ?? $this->getDefaultScope()) { case self::SCOPE_GLOBAL: diff --git a/tests/UnitTests/SmartyMethodsTests/Assign/AssignTest.php b/tests/UnitTests/SmartyMethodsTests/Assign/AssignTest.php index ea5f3a4bc..e8ae92b73 100644 --- a/tests/UnitTests/SmartyMethodsTests/Assign/AssignTest.php +++ b/tests/UnitTests/SmartyMethodsTests/Assign/AssignTest.php @@ -42,4 +42,15 @@ public function testArrayAssign() $this->smarty->assign(array('foo' => 'bar', 'foo2' => 'bar2')); $this->assertEquals('bar bar2', $this->smarty->fetch('eval:{$foo} {$foo2}')); } + + /** + * Test that assign returns this. + */ + public function testAssignReturnsThis() + { + $this->assertEquals( + 'data', + $this->smarty->assign(['dummy' => 'data'])->fetch('eval:{$dummy}') + ); + } }