-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathView.php
More file actions
40 lines (32 loc) · 687 Bytes
/
Copy pathView.php
File metadata and controls
40 lines (32 loc) · 687 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
declare(strict_types=1);
class ViewDoesNotExistException extends Exception {}
class View
{
public static function load(string $view_file, array $data = [])
{
$view_file = 'views/' . $view_file;
if (!file_exists($view_file))
{
throw new ViewDoesNotExistException("View file '$view_file' does not exist.");
}
foreach ($data as $key => $value)
{
${$key} = $value;
}
ob_start();
include($view_file);
return ob_get_clean();
}
public static function display(string $view_file, $data = [])
{
try
{
echo View::load($view_file, $data);
}
catch (ViewDoesNotExistException $e)
{
echo "Loading view '$view_file' failed.";
}
}
}