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 view.base_path and shares framework helpers/services into templates: helpers, component, baseUrl, url and csrf.
View configuration
view.base_path defines the root directory for PHP templates. 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.
return [
'view' => [
'base_path' => Env::string('VIEW_BASE_PATH', '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.
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().
$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.
/**
* @var string $title
*/
?>
<h1><?= e($title) ?></h1>
View helpers
ViewHelpers is shared as $helpers. It exposes framework helpers: asset(), url(), localizedUrl(), csrfField(), csrfToken(), lang(), currentLocale(), langGroup() and langAll(). Request-specific helpers may be added by the HTTP/controller layer during rendering.
<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.
$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().
<?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.
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.