Adds global data to view
This commit is contained in:
+15
-4
@@ -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!');
|
||||
|
||||
Reference in New Issue
Block a user