Fixed issue with data in view class

pull/650/head
n0nag0n 2 weeks ago
parent aaf9f62ecc
commit c470a65a8e

@ -103,11 +103,11 @@ class View
* Renders a template.
*
* @param string $file Template file
* @param ?array<string, mixed> $data Template data
* @param ?array<string, mixed> $templateData Template data
*
* @throws \Exception If template not found
*/
public function render(string $file, ?array $data = null): void
public function render(string $file, ?array $templateData = null): void
{
$this->template = $this->getTemplate($file);
@ -118,11 +118,11 @@ class View
\extract($this->vars);
if (\is_array($data) === true) {
\extract($data);
if (\is_array($templateData) === true) {
\extract($templateData);
if ($this->preserveVars === true) {
$this->vars = \array_merge($this->vars, $data);
$this->vars = \array_merge($this->vars, $templateData);
}
}

@ -63,6 +63,7 @@ class LayoutMiddleware
<li><a href="/alias">Alias Route</a></li>
<li><a href="/protected">Protected path</a></li>
<li><a href="/template/templatevariable">Template path</a></li>
<li><a href="/template-data/template-data-variable">Template Data Variable</a></li>
<li><a href="/querytestpath?test=1&variable2=uuid&variable3=tester">Query path</a></li>
<li><a href="/badpagename">404 Not Found</a></li>
<li><a href="/postpage">405 Method Not Found</a></li>

@ -98,6 +98,10 @@ Flight::group('', function () {
Flight::render('template.phtml', ['name' => $name]);
});
Flight::route('/template-data/@data', function ($data) {
Flight::render('template.phtml', ['data' => $data]);
});
// Test 8: Throw an error
Flight::route('/error', function () {
trigger_error('This is a successful error');

@ -1 +1,5 @@
<?php if(isset($name)): ?>
<span id="infotext">Route text:</span> Template <?=$name?> works!
<?php elseif(isset($data)): ?>
<span id="infotext">Route text:</span> Template with variable name "data" works! See: <?=$data?>
<?php endif; ?>

Loading…
Cancel
Save