Implement simple PHP based view renderer

This commit is contained in:
Tim
2023-08-15 03:02:55 +02:00
parent 9b8985640c
commit c80dcd2860
9 changed files with 83 additions and 9 deletions

32
src/View/BaseView.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace Ardent\Undercurrent\View;
use Ardent\Undercurrent\AppConfig;
use Ardent\Undercurrent\Http\GenericResponse;
use Ardent\Undercurrent\Http\ResponseInterface;
class BaseView implements ViewInterface
{
public function __construct(
private readonly string $path,
private readonly array $data = [],
private ?string $extends = null,
)
{
}
public function render(AppConfig $config): string
{
$path = $config->getRootPath() . $this->path;
ob_start();
include $path;
$output = ob_get_clean();
if ($this->extends) {
$output = (new BaseView($this->extends, $this->data + ['slot' => $output]))->render($config);
}
return $output;
}
}