Presentation

View Rendering

Napojení view engine, shared variables, component integration a rendering workflow.

Overview

View module provides PHP template rendering. It renders .php files, supports shared data, layout extension and named sections.

View location

Default view base path is configured via view.base_path and defaults to app/Views (from app/Config/App.php). Dot notation maps to directories, for example home.index -> app/Views/home/index.php.

Registered services

ViewServiceProvider registers Lemonade\Framework\View\View::class (no string alias). It shares app-scoped ViewHelpers into templates as $helpers. Request/session-dependent RequestViewHelpers is created per controller request/render and shared as $requestHelpers by the controller view lifecycle.

$view = new View($basePath);

$view->share('helpers', $container->get(ViewHelpers::class));

// ControllerServices shares requestHelpers for the current request only.

Rendering from a controller

Base Controller exposes view() helper and response helpers such as html().

$html = $this->view()->render('home.index', [
    'title' => 'Homepage',
]);

return $this->html($html);

Rendering through dependency injection

For non-controller rendering, inject View directly.

final class SitemapPageRenderer
{
    public function __construct(
        private readonly View $views,
    ) {
    }

    public function render(array $pages): string
    {
        return $this->views->render('sitemap.index', [
            'pages' => $pages,
        ]);
    }
}

View files

Templates are plain PHP files. Escape dynamic output explicitly and document shared template variables for IDE/PHPStan.

<?php

/**
 * @var \Lemonade\Framework\View\View $this
 * @var \Lemonade\Framework\View\ViewHelpers $helpers
 * @var \Lemonade\Framework\View\RequestViewHelpers $requestHelpers
 * @var array{title:string} $page
 */
?>

<h1><?= htmlspecialchars($title, ENT_QUOTES, 'UTF-8') ?></h1>

View helpers

Inside templates use $helpers for app-scoped helpers and $requestHelpers for current request/session state.

<a href="<?= htmlspecialchars($helpers->url('home'), ENT_QUOTES, 'UTF-8') ?>">Home</a>
<?= $helpers->csrfField() ?>

<input name="email" value="<?= htmlspecialchars((string) $requestHelpers->old('email'), ENT_QUOTES, 'UTF-8') ?>">

Layouts and sections

Renderer supports extend() and section API (start(), end(), section(), content()). It also provides template(layout, content, data) for explicit layout wrapping and partial() for fragment rendering.

// content view
$this->extend('layouts.main');
$this->start('body');
echo '<p>Hello</p>';
$this->end();

// layout view
<?= $this->section('body') ?>

Components integration

View module integrates with ComponentRegistry through shared variable component. Framework components and custom configured components can be resolved there; see Components documentation for registration details.