From f3453cab178809216f71ed832d041bece1b8a33a Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Sat, 1 Jun 2024 21:07:55 -0400 Subject: [PATCH] Simplified fix thanks to @vlakoff (https://github.com/vlakoff) --- flight/template/View.php | 14 ++++++-------- tests/ViewTest.php | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/flight/template/View.php b/flight/template/View.php index f78a968..d347a50 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -116,19 +116,17 @@ class View throw new \Exception("Template file not found: {$normalized_path}."); } - if (\is_array($data)) { - $this->vars = \array_merge($this->vars, $data); - } - \extract($this->vars); - include $this->template; + if (\is_array($data)) { + \extract($data); - if ($this->preserveVars === false && $data !== null) { - foreach (array_keys($data) as $variable) { - unset($this->vars[$variable]); + if ($this->preserveVars) { + $this->vars = \array_merge($this->vars, $data); } } + + include $this->template; } /** diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 38cabe0..d6754c9 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -191,6 +191,22 @@ class ViewTest extends TestCase $this->view->render('input'); } + public function testKeepThePreviousStateOfDataSettedBySetMethod(): void + { + $this->view->preserveVars = false; + + $this->view->set('prop', 'bar'); + + $this->expectOutputString(<<qux +
bar
+ + html); + + $this->view->render('myComponent', ['prop' => 'qux']); + $this->view->render('myComponent'); + } + public static function renderDataProvider(): array { return [