40 lines
1.0 KiB
PHP
Executable File
40 lines
1.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Core;
|
|
|
|
class View
|
|
{
|
|
public function renderView($view, $params = [])
|
|
{
|
|
$viewContent = $this->renderOnlyView($view, $params);
|
|
$layoutContent = $this->layoutContent();
|
|
return str_replace('{{content}}', $viewContent, $layoutContent);
|
|
}
|
|
|
|
protected function layoutContent()
|
|
{
|
|
ob_start();
|
|
include_once Application::$ROOT_DIR . "/app/Views/layouts/main.php";
|
|
return ob_get_clean();
|
|
}
|
|
|
|
protected function renderOnlyView($view, $params)
|
|
{
|
|
foreach ($params as $key => $value) {
|
|
$$key = $value;
|
|
}
|
|
ob_start();
|
|
include_once Application::$ROOT_DIR . "/app/Views/$view.php";
|
|
$content = ob_get_clean();
|
|
|
|
// Simple {{variable}} replacement
|
|
foreach ($params as $key => $value) {
|
|
if (is_scalar($value)) {
|
|
$content = str_replace('{{'.$key.'}}', $value, $content);
|
|
}
|
|
}
|
|
|
|
return $content;
|
|
}
|
|
}
|