|
1 | 1 | <?php
|
| 2 | +use Monster\App\Models\Env; |
2 | 3 |
|
3 | 4 | /*
|
4 | 5 | Application helpers
|
5 | 6 | */
|
6 | 7 |
|
7 |
| -// Load Thems |
8 |
| -function view($path, $data = [], $javascript = false) |
| 8 | +// Load Views |
| 9 | +function view($path, $data = [], $javascript = false, $tcss = false) |
9 | 10 | {
|
| 11 | + $env = new Env('.env'); |
| 12 | + $tcss_path = $env->get("TCSS_MIN"); |
| 13 | + |
10 | 14 | // Replace all . with /
|
11 | 15 | $path = str_replace('.', '/', $path);
|
12 | 16 |
|
13 | 17 | extract($data);
|
14 | 18 |
|
15 |
| - // include views folder path |
| 19 | + // Include views folder path |
16 | 20 | $viewPath = 'views/' . $path . '.php';
|
17 | 21 |
|
| 22 | + // Check if the view file exists |
| 23 | + if (!file_exists($viewPath)) { |
| 24 | + // Handle the error, e.g., throw an exception or show a 404 error |
| 25 | + exit('View does not exist.'); |
| 26 | + } |
| 27 | + |
18 | 28 | // Wrap the view rendering code in a buffer
|
19 | 29 | ob_start();
|
20 | 30 | include_once $viewPath;
|
21 | 31 | $viewContent = ob_get_clean();
|
22 | 32 |
|
23 |
| - if ($javascript == "true") { |
24 |
| - $viewContent = str_replace('<title>', "<script>let monster = JSON.parse('" . json_encode($data) . "');</script>\n<title>", $viewContent); |
| 33 | + // Inject JavaScript if $javascript is true |
| 34 | + if ($javascript) { |
| 35 | + $encodedData = json_encode($data); |
| 36 | + $viewContent = str_replace('<title>', "<script>let monster = JSON.parse('{$encodedData}');</script>\n<title>", $viewContent); |
| 37 | + } |
| 38 | + |
| 39 | + // Inject CSS if $tcss is true |
| 40 | + if ($tcss) { |
| 41 | + // Assuming you have a specific CSS file to include |
| 42 | + $cssLink = '<link rel="stylesheet" href="' . $tcss_path . '">'; |
| 43 | + $viewContent = str_replace('<title>', "{$cssLink}\n<title>", $viewContent); |
25 | 44 | }
|
26 | 45 |
|
27 | 46 | echo $viewContent;
|
|
0 commit comments