Core

View Service Provider

PHP view renderer, helpers, shared data and layout composition.

Responsibility

Registers Lemonade\Framework\View\ViewHelpers and Lemonade\Framework\View\View. ViewServiceProvider creates the PHP view renderer from ViewConfig::$basePath and shares framework helpers/services into templates: helpers, component, baseUrl, url and csrf.

View configuration

View base path is configured in app/Config/View.yaml. YAML is mapped into typed ViewConfigDefinition, and ViewServiceProvider then builds the runtime View from ViewConfig::$basePath. Dot notation maps to files below that base path: pages.home resolves to <base_path>/pages/home.php, and layouts.app resolves to <base_path>/layouts/app.php.

example.php
module: view
config:
  base_path:
    $env: VIEW_BASE_PATH
    type: string
    default: app/Views

Rendering

View::render() renders one template and returns HTML as string. Data is passed as an associative array and extracted into local template variables.

example.php
use Lemonade\Framework\View\View;

$html = $view->render('pages.home', [
    'title' => 'Home',
]);

Layouts

View::template($layoutView, $contentView, $data) renders the content view first, stores it as layout content, then renders the layout. The layout can output the main content through $this->content().

example.php
$html = $view->template('layouts.app', 'pages.home', [
    'title' => 'Home',
]);

// layouts/app.php
<main>
    <?= $this->content() ?>
</main>

Template data

Template data keys become local variables inside the PHP template. Document expected variables with PHPDoc and escape dynamic output with the global e() helper.

example.php
/**
 * @var string $title
 */
?>

<h1><?= e($title) ?></h1>

View helpers

ViewHelpers is shared as $helpers. It exposes framework helper methods for assets, URLs, localized URLs, CSRF fields, CSRF tokens, translations, current locale and translation groups. Request-specific helpers may be added by the HTTP/controller layer during rendering.

example.php
<a href="<?= e($helpers->localizedUrl('home')) ?>">
    <?= e($helpers->lang('messages.home')) ?>
</a>

<?= $helpers->csrfField() ?>

Shared data

View::share() stores data available to every render, shareOnce() stores data only for the current top-level render, and shares() merges multiple shared values. ViewServiceProvider uses this mechanism for framework helpers and shared services.

example.php
$view->share('siteName', 'Example App');
$view->shareOnce('requestId', $requestId);

$html = $view->render('pages.home', [
    'title' => 'Home',
]);

Sections and content

View supports named slots through start(), end() and section(). It also supports extend() for layout-style rendering and content() for the main rendered body used by template() or extend().

example.php
<?php $this->start('sidebar'); ?>
    <nav>Sidebar</nav>
<?php $this->end(); ?>

<!-- layout -->
<?= $this->section('sidebar') ?>

<main>
    <?= $this->content() ?>
</main>

Controller usage

Controllers can inject View directly or use a framework/base controller helper when the application provides one. Returning the HTML as a PSR-7 response is the responsibility of the application response factory or controller base.

example.php
use Lemonade\Framework\View\View;
use Psr\Http\Message\ResponseInterface;

final class HomeController
{
    public function __construct(
        private readonly View $view,
    ) {}

    public function index(): ResponseInterface
    {
        $html = $this->view->template('layouts.app', 'pages.home', [
            'title' => 'Home',
        ]);

        // Return a PSR-7 HTML response according to the app response layer.
    }
}

Error views

Error rendering can use the same View service, but concrete error templates and layouts are application-defined. The view provider only supplies the renderer and shared helpers.