Skip to content

Commit 5df2800

Browse files
authored
Merge pull request #650 from flightphp/bad-data-var-in-view
Fixed issue with data in view class
2 parents aaf9f62 + c470a65 commit 5df2800

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

flight/template/View.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ public function clear(?string $key = null): self
103103
* Renders a template.
104104
*
105105
* @param string $file Template file
106-
* @param ?array<string, mixed> $data Template data
106+
* @param ?array<string, mixed> $templateData Template data
107107
*
108108
* @throws \Exception If template not found
109109
*/
110-
public function render(string $file, ?array $data = null): void
110+
public function render(string $file, ?array $templateData = null): void
111111
{
112112
$this->template = $this->getTemplate($file);
113113

@@ -118,11 +118,11 @@ public function render(string $file, ?array $data = null): void
118118

119119
\extract($this->vars);
120120

121-
if (\is_array($data) === true) {
122-
\extract($data);
121+
if (\is_array($templateData) === true) {
122+
\extract($templateData);
123123

124124
if ($this->preserveVars === true) {
125-
$this->vars = \array_merge($this->vars, $data);
125+
$this->vars = \array_merge($this->vars, $templateData);
126126
}
127127
}
128128

tests/server/LayoutMiddleware.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function before(): void
6363
<li><a href="/alias">Alias Route</a></li>
6464
<li><a href="/protected">Protected path</a></li>
6565
<li><a href="/template/templatevariable">Template path</a></li>
66+
<li><a href="/template-data/template-data-variable">Template Data Variable</a></li>
6667
<li><a href="/querytestpath?test=1&variable2=uuid&variable3=tester">Query path</a></li>
6768
<li><a href="/badpagename">404 Not Found</a></li>
6869
<li><a href="/postpage">405 Method Not Found</a></li>

tests/server/index.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@
9898
Flight::render('template.phtml', ['name' => $name]);
9999
});
100100

101+
Flight::route('/template-data/@data', function ($data) {
102+
Flight::render('template.phtml', ['data' => $data]);
103+
});
104+
101105
// Test 8: Throw an error
102106
Flight::route('/error', function () {
103107
trigger_error('This is a successful error');

tests/server/template.phtml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
<?php if(isset($name)): ?>
12
<span id="infotext">Route text:</span> Template <?=$name?> works!
3+
<?php elseif(isset($data)): ?>
4+
<span id="infotext">Route text:</span> Template with variable name "data" works! See: <?=$data?>
5+
<?php endif; ?>

0 commit comments

Comments
 (0)