Chapter 6 of 15
Views and layouts.
Render PHP templates with explicit data, a shared layout, and safe output escaping.
Rendering from a controller
The injected ViewRendererInterface runs in the Request scope. render() returns a PSR-7 response; content() returns rendered HTML for composition.
Controller
<?php
declare(strict_types=1);
use Lemonade\Framework\View\ViewRendererInterface;
use Psr\Http\Message\ResponseInterface;
final readonly class ArticleController
{
public function __construct(
private ViewRendererInterface $views,
private ArticleRepository $articles,
) {}
public function detail(string $id): ResponseInterface
{
return $this->views->render('articles.detail', [
'article' => $this->articles->get($id),
]);
}
}The configured view resources resolve articles.detail to a PHP file.
Templates and helpers
Pass only the data a view needs and escape dynamic text. Use delegated methods on $this for URLs, translations, assets, and CSRF; $requestHelpers exposes request-local data.
app/Views/articles/detail.php
<?php
declare(strict_types=1);
/** @var array{id:string,title:string,body:string} $article */ ?>
<article>
<h1><?= e($article['title']) ?></h1>
<p><?= e($article['body']) ?></p>
</article>Layouts, partials, and sections remain available through the view object in $this.