Presentation

View Rendering

Wiring der view engine, shared variables, component integration und 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 in app/Config/View.yaml and defaults to app/Views. YAML is mapped into typed ViewConfigDefinition during config loading. 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.

example.php
$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().

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

return $this->html($html);

Rendering through dependency injection

For non-controller rendering, inject View directly.

example.php
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.

example.php
<?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.

example.php
<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.

example.php
// 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.