Adds global data to view

This commit is contained in:
2026-04-01 17:55:24 +02:00
parent 95f00fb15e
commit cbd7848a93
+15 -4
View File
@@ -15,9 +15,12 @@ class View {
private string $defaultBaseTemplate;
private array $globalData = [];
/**
* @throws Exception
*/
public function __construct(string $templateDirectory, ?string $defaultBaseTemplate = null) {
if (!file_exists($templateDirectory)) {
throw new Exception('Template dorectory not found!');
throw new Exception('Template directory not found!');
}
$this->templateDirectory = $templateDirectory;
if ($defaultBaseTemplate) {
@@ -28,8 +31,12 @@ class View {
}
}
/**
* @throws Exception
*/
public function render(string $view, array $data = [], ?string $base = null) {
extract($data);
$completeData = array_merge($this->globalData, $data);
extract($completeData);
if (!file_exists($this->templateDirectory . '/' . $view . '.php')) {
throw new Exception('Template not found!');
@@ -55,8 +62,12 @@ class View {
}
}
public function return(string $view, array $data = [], ?string $base = null): string|false {
extract($data);
/**
* @throws Exception
*/
public function return(string $view, array $data = [], ?string $base = null): string {
$completeData = array_merge($this->globalData, $data);
extract($completeData);
if (!file_exists($this->templateDirectory . '/' . $view . '.php')) {
throw new Exception('Template not found!');