Add ViewHelper in view for injecting some basic classes in views
This commit is contained in:
@ -3,28 +3,29 @@
|
||||
namespace Ardent\Undercurrent\View;
|
||||
|
||||
use Ardent\Undercurrent\AppConfig;
|
||||
use Ardent\Undercurrent\Http\GenericResponse;
|
||||
use Ardent\Undercurrent\Http\ResponseInterface;
|
||||
|
||||
class BaseView implements ViewInterface
|
||||
{
|
||||
private ViewHelper $helper;
|
||||
|
||||
public function __construct(
|
||||
private readonly string $path,
|
||||
private readonly array $data = [],
|
||||
private ?string $extends = null,
|
||||
private readonly string $path,
|
||||
private readonly array $data = [],
|
||||
private ?string $extends = null,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function render(AppConfig $config): string
|
||||
public function render(AppConfig $config, ViewHelper $helper): string
|
||||
{
|
||||
$path = $config->getRootPath() . $this->path;
|
||||
$this->helper = $helper;
|
||||
$path = $config->getTemplatePath() . $this->path;
|
||||
|
||||
ob_start();
|
||||
include $path;
|
||||
$output = ob_get_clean();
|
||||
if ($this->extends) {
|
||||
return (new BaseView($this->extends, $this->data + ['slot' => $output]))->render($config);
|
||||
return (new BaseView($this->extends, $this->data + ['slot' => $output]))->render($config, $helper);
|
||||
}
|
||||
|
||||
return $output;
|
||||
|
19
src/View/ViewHelper.php
Normal file
19
src/View/ViewHelper.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Ardent\Undercurrent\View;
|
||||
|
||||
use Ardent\Undercurrent\Http\RouterInterface;
|
||||
|
||||
class ViewHelper
|
||||
{
|
||||
public function __construct(
|
||||
private readonly RouterInterface $router,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function getRoute(string $class, string $method, array $params = []): string
|
||||
{
|
||||
return $this->router->toUri($class, $method, $params);
|
||||
}
|
||||
}
|
@ -7,5 +7,5 @@ use Ardent\Undercurrent\Http\ResponseInterface;
|
||||
|
||||
interface ViewInterface
|
||||
{
|
||||
public function render(AppConfig $config): string;
|
||||
public function render(AppConfig $config, ViewHelper $helper): string;
|
||||
}
|
Reference in New Issue
Block a user