32 lines
715 B
PHP
32 lines
715 B
PHP
|
|
<?php
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Core\Views;
|
||
|
|
|
||
|
|
class View
|
||
|
|
{
|
||
|
|
private string $basePath;
|
||
|
|
|
||
|
|
public function __construct(string $basePath = '')
|
||
|
|
{
|
||
|
|
$this->basePath = $basePath !== '' ? rtrim($basePath, '/') : __DIR__ . '/../../views';
|
||
|
|
}
|
||
|
|
|
||
|
|
public function render(string $template, array $vars = []): string
|
||
|
|
{
|
||
|
|
$path = $this->basePath !== '' ? $this->basePath . '/' . ltrim($template, '/') : $template;
|
||
|
|
if (!is_file($path)) {
|
||
|
|
error_log('AC View missing: ' . $path);
|
||
|
|
return '';
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($vars) {
|
||
|
|
extract($vars, EXTR_SKIP);
|
||
|
|
}
|
||
|
|
|
||
|
|
ob_start();
|
||
|
|
require $path;
|
||
|
|
return ob_get_clean() ?: '';
|
||
|
|
}
|
||
|
|
}
|