48 lines
1.1 KiB
PHP

<?php
/*
* Copyright (c) 2022 Micha Espey <tracer@24unix.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Service;
/*
* As I'm not allowed to use 3rd party code like Twig or Smarty I ended up
* using PHP as a templating engine.
*/
use JetBrains\PhpStorm\NoReturn;
class Template
{
/*
* Just store the information about the template base dir.
*/
public function __construct(private readonly string $templateDir)
{
// empty body
}
/*
* Add variables to template and throw it out
*/
#[NoReturn]
public function render(string $templateName, array $vars = []): void
{
// assign template vars
foreach ($vars as $name => $value) {
$$name = $value;
}
$templateFile = $this->templateDir . $templateName;
if (file_exists(filename: $templateFile)) {
include $this->templateDir . $templateName;
} else {
die("Missing template: $templateFile");
}
exit(0);
}
}