die with error message if template doesn't exist.

This commit is contained in:
tracer 2022-10-23 18:46:12 +02:00
parent b60d9481ab
commit 33618b3be0
1 changed files with 11 additions and 1 deletions

View File

@ -12,6 +12,9 @@ namespace App\Service;
* As I'm not allowed to use 3rd party code like Twig or Smarty I ended up * As I'm not allowed to use 3rd party code like Twig or Smarty I ended up
* using PHP as a templating engine. * using PHP as a templating engine.
*/ */
use JetBrains\PhpStorm\NoReturn;
class Template class Template
{ {
/* /*
@ -25,13 +28,20 @@ class Template
/* /*
* Add variables to template and throw it out * Add variables to template and throw it out
*/ */
#[NoReturn]
public function render(string $templateName, array $vars = []): void public function render(string $templateName, array $vars = []): void
{ {
// assign template vars
foreach ($vars as $name => $value) { foreach ($vars as $name => $value) {
$$name = $value; $$name = $value;
} }
$templateFile = $this->templateDir . $templateName;
if (file_exists(filename: $templateFile)) {
include $this->templateDir . $templateName; include $this->templateDir . $templateName;
} else {
die("Missing template: $templateFile");
}
exit(0); exit(0);
} }
} }