Repository navigation
Expand file tree
/
Copy pathView.php
More file actions
53 lines (48 loc) · 1.42 KB
/
Copy pathView.php
File metadata and controls
53 lines (48 loc) · 1.42 KB
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
<?php
/**
* Project: armin - Filename: View.php
* Namespace: agellweiler\phpmvc
* Initial version by: Armin Gellweiler, E-Mail: armin@gellweiler.net
* Company: igus GmbH
* Initial version created on: 29.01.22 22:12
*/
namespace agellweiler\phpmvc;
/**
* Class View
* @package agellweiler\phpmvc
* @author Armin Gellweiler <armin@gellweiler.net>
*/
class View
{
public string $title = '';
public function renderView($view, $params = [])
{
$viewContent = $this->renderOnlyView($view, $params);
$layoutContent = $this->layoutContent();
return str_replace('{{content}}', $viewContent, $layoutContent);
}
public function renderContent(string $viewContent)
{
$layoutContent = $this->layoutContent();
return str_replace('{{content}}', $viewContent, $layoutContent);
}
protected function layoutContent()
{
$layout = Application::$app->layout;
if (Application::$app->controller) {
$layout = Application::$app->controller->layout;
}
ob_start();
include_once Application::$ROOT_DIR."/views/layouts/$layout.php";
return ob_get_clean();
}
protected function renderOnlyView(string $view, $params)
{
foreach ($params as $key => $value) {
$$key = $value;
}
ob_start();
include_once Application::$ROOT_DIR."/views/$view.php";
return ob_get_clean();
}
}