Skip to content

Latest commit

 

History

History
53 lines (42 loc) · 1.32 KB

File metadata and controls

53 lines (42 loc) · 1.32 KB

Custom Renderer

Custom renderers implement RendererInterface. Extending AbstractRenderer gives you common normalization helpers.

use CommonPHP\UI\Contracts\AbstractRenderer;
use CommonPHP\UI\Contracts\ComponentInterface;
use CommonPHP\UI\Contracts\TemplateInterface;
use CommonPHP\UI\View;
use CommonPHP\UI\ViewData;

final class ArrayRenderer extends AbstractRenderer
{
    public function render(View $view): string
    {
        return $this->renderTemplate(
            $view->template(),
            $view->data(),
        );
    }

    public function renderTemplate(TemplateInterface|string $template, array|ViewData $data = []): string
    {
        $template = $this->template($template);
        $payload = $this->mergedData($template->data(), $data);

        return json_encode([
            'template' => $template->name(),
            'data' => $payload->all(),
        ], JSON_THROW_ON_ERROR);
    }

    public function renderComponent(ComponentInterface|string $component, array|ViewData $data = []): string
    {
        $component = $this->component($component);

        return $this->renderTemplate($component, $data);
    }
}

Use it directly:

$ui = new ViewFactory(new ArrayRenderer());

Or through runtime driver integration:

$ui = new ViewFactory();
$ui->setDriver(ArrayRenderer::class);