Added flag to not break Flight users code

pull/581/head
fadrian06 9 months ago
parent 1b6cb088b7
commit eadf433334

@ -20,6 +20,8 @@ class View
/** File extension. */ /** File extension. */
public string $extension = '.php'; public string $extension = '.php';
public bool $preserveVars = true;
/** /**
* View variables. * View variables.
* *
@ -122,7 +124,7 @@ class View
include $this->template; include $this->template;
if ($data !== null) { if ($this->preserveVars === false && $data !== null) {
foreach (array_keys($data) as $variable) { foreach (array_keys($data) as $variable) {
unset($this->vars[$variable]); unset($this->vars[$variable]);
} }

@ -22,6 +22,7 @@ class FlightTest extends TestCase
$_REQUEST = []; $_REQUEST = [];
Flight::init(); Flight::init();
Flight::setEngine(new Engine()); Flight::setEngine(new Engine());
Flight::set('flight.views.path', __DIR__ . '/views');
} }
protected function tearDown(): void protected function tearDown(): void
@ -355,9 +356,9 @@ class FlightTest extends TestCase
$this->expectOutputString('Thisisaroutewithhtml'); $this->expectOutputString('Thisisaroutewithhtml');
} }
public function testItDoesNotKeepThePreviousStateOfOneViewComponentUsingFlightRender(): void public function testDoesNotPreserveVarsWhenFlagIsDisabled(): void
{ {
Flight::set('flight.views.path', __DIR__ . '/views'); Flight::view()->preserveVars = false;
$this->expectOutputString("<div>Hi</div>\n<div></div>\n"); $this->expectOutputString("<div>Hi</div>\n<div></div>\n");
Flight::render('myComponent', ['prop' => 'Hi']); Flight::render('myComponent', ['prop' => 'Hi']);
@ -370,4 +371,11 @@ class FlightTest extends TestCase
restore_error_handler(); restore_error_handler();
} }
public function testKeepThePreviousStateOfOneViewComponentByDefault(): void
{
$this->expectOutputString("<div>Hi</div>\n<div>Hi</div>\n");
Flight::render('myComponent', ['prop' => 'Hi']);
Flight::render('myComponent');
}
} }

@ -153,8 +153,10 @@ class ViewTest extends TestCase
); );
} }
public function testItDoesNotKeepThePreviousStateOfOneViewComponent(): void public function testDoesNotPreserveVarsWhenFlagIsDisabled(): void
{ {
$this->view->preserveVars = false;
$this->expectOutputString("<div>Hi</div>\n<div></div>\n"); $this->expectOutputString("<div>Hi</div>\n<div></div>\n");
$this->view->render('myComponent', ['prop' => 'Hi']); $this->view->render('myComponent', ['prop' => 'Hi']);
@ -166,4 +168,11 @@ class ViewTest extends TestCase
restore_error_handler(); restore_error_handler();
} }
public function testKeepThePreviousStateOfOneViewComponentByDefault(): void
{
$this->expectOutputString("<div>Hi</div>\n<div>Hi</div>\n");
$this->view->render('myComponent', ['prop' => 'Hi']);
$this->view->render('myComponent');
}
} }

Loading…
Cancel
Save