-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.php
50 lines (44 loc) · 1.61 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
<?php
namespace Okami\Core;
/**
* Class View
*
* @author Michal Tuček <michaltk1@gmail.com>
* @package Okami\Core
*/
class View
{
public string $title = '';
public function renderView(string $view, array $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 = App::$app->layout;
if (App::$app->getController()) {
$layout = App::$app->getController()->getLayout();
}
ob_start(); // This will stop everything from being displays but still buffers it
/** @noinspection PhpIncludeInspection */
include_once App::$ROOT_DIR . "/views/layouts/$layout.phtml";
return ob_get_clean(); // Returns the content of the "display" buffer
}
protected function renderOnlyView(string $view, array $params)
{
foreach ($params as $param => $value) {
$$param = $value; // If $param can be used as a variable name, then created one and fill it with the value
}
ob_start(); // This will stop everything from being displays but still buffers it
/** @noinspection PhpIncludeInspection */
include_once App::$ROOT_DIR . "/views/$view.phtml";
return ob_get_clean(); // Returns the content of the "display" buffer
}
}