-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.php
69 lines (58 loc) · 1.42 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
61
62
63
64
65
66
67
68
69
<?php
namespace hussainalihussain\phpmvclaravelclonecore;
class View
{
public $layout;
public $title = 'Title goes here';
/**
* @param string $layout
*/
public function __construct(string $layout = 'main')
{
$this->setLayout($layout);
}
/**
* @param string $layout
* @return void
*/
public function setLayout(string $layout)
{
$this->layout = $layout;
}
/**
* @param string $view
* @param array $params
* @return array|false|string|string[]
*/
public function renderView(string $view, array $params = [])
{
$layout = $this->renderLayout();
$view = str_replace('.', '/', $view);
$viewContent = $this->getViewContent($view, $params);
return str_replace('{{content}}', $viewContent, $layout);
}
/**
* @return false|string
*/
public function renderLayout()
{
ob_start();
require Application::$ROOT_PATH . "/views/layouts/{$this->layout}.php";
return ob_get_clean();
}
/**
* @param $view
* @param $params
* @return false|string
*/
public function getViewContent($view, $params = [])
{
foreach ($params as $variable=> $value)
{
${$variable} = $value;
}
ob_start();
require Application::$ROOT_PATH . "/views/{$view}.php";
return ob_get_clean();
}
}