-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.php
60 lines (55 loc) · 1.34 KB
/
View.php
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace alvin\phpmvc;
/**
* Class for rendering html content
*/
class View {
/**
* Name of the layout file to use
* @var string
*/
public string $layout = 'index';
/**
* Sets a layout.
*
* @param string $layout
* @return void
*/
public function setLayout(string $layout) {
$this->layout = $layout;
}
/**
* Renders a view with a layout.
*
* @param string $view Name of the view file
* @param array $params parameters to pass into the view
* @return string
*/
public function renderView(string $view, array $params) {
ob_start();
include Application::$app->rootDir . "views/layouts/$this->layout.php";
$layoutContent = ob_get_clean();
$viewContent = $this->renderViewOnly($view, $params);
return str_replace('{{content}}', $viewContent, $layoutContent);
}
/**
* Renders a view without a layout.
*
* @param string $view Name of the view file
* @param array $params parameters to pass into the view
* @return string
*/
public function renderViewOnly(string $view, array $params = []) {
foreach ($params as $key => $value) {
$$key = $value;
}
$_FLASH = [];
foreach ($_SESSION['flash'] as $key => $value) {
$_FLASH[$key] = $value['value'];
}
$_csrfToken = $_SESSION['csrfToken'];
ob_start();
include_once Application::$app->rootDir . "views/$view.php";
return ob_get_clean();
}
}