Implement simple PHP based view renderer
This commit is contained in:
32
src/View/BaseView.php
Normal file
32
src/View/BaseView.php
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user