From eadf4333342e445e33800f34b23b3855541f2a19 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Wed, 24 Apr 2024 20:33:12 -0400 Subject: [PATCH] Added flag to not break Flight users code --- flight/template/View.php | 4 +++- tests/FlightTest.php | 12 ++++++++++-- tests/ViewTest.php | 11 ++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/flight/template/View.php b/flight/template/View.php index 9397cc6..f78a968 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -20,6 +20,8 @@ class View /** File extension. */ public string $extension = '.php'; + public bool $preserveVars = true; + /** * View variables. * @@ -122,7 +124,7 @@ class View include $this->template; - if ($data !== null) { + if ($this->preserveVars === false && $data !== null) { foreach (array_keys($data) as $variable) { unset($this->vars[$variable]); } diff --git a/tests/FlightTest.php b/tests/FlightTest.php index fe5b144..f9f2855 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -22,6 +22,7 @@ class FlightTest extends TestCase $_REQUEST = []; Flight::init(); Flight::setEngine(new Engine()); + Flight::set('flight.views.path', __DIR__ . '/views'); } protected function tearDown(): void @@ -355,9 +356,9 @@ class FlightTest extends TestCase $this->expectOutputString('Thisisaroutewithhtml'); } - public function testItDoesNotKeepThePreviousStateOfOneViewComponentUsingFlightRender(): void + public function testDoesNotPreserveVarsWhenFlagIsDisabled(): void { - Flight::set('flight.views.path', __DIR__ . '/views'); + Flight::view()->preserveVars = false; $this->expectOutputString("
Hi
\n
\n"); Flight::render('myComponent', ['prop' => 'Hi']); @@ -370,4 +371,11 @@ class FlightTest extends TestCase restore_error_handler(); } + + public function testKeepThePreviousStateOfOneViewComponentByDefault(): void + { + $this->expectOutputString("
Hi
\n
Hi
\n"); + Flight::render('myComponent', ['prop' => 'Hi']); + Flight::render('myComponent'); + } } diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 2432f15..361bb3c 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -153,8 +153,10 @@ class ViewTest extends TestCase ); } - public function testItDoesNotKeepThePreviousStateOfOneViewComponent(): void + public function testDoesNotPreserveVarsWhenFlagIsDisabled(): void { + $this->view->preserveVars = false; + $this->expectOutputString("
Hi
\n
\n"); $this->view->render('myComponent', ['prop' => 'Hi']); @@ -166,4 +168,11 @@ class ViewTest extends TestCase restore_error_handler(); } + + public function testKeepThePreviousStateOfOneViewComponentByDefault(): void + { + $this->expectOutputString("
Hi
\n
Hi
\n"); + $this->view->render('myComponent', ['prop' => 'Hi']); + $this->view->render('myComponent'); + } }