Skip to content

Commit 29a7ae9

Browse files
[FrameworkBundle] Add AbstractController::renderBlock() and $block argument to renderView()
1 parent f0959b4 commit 29a7ae9

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

UPGRADE-6.4.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ FrameworkBundle
7878
---------------
7979

8080
* [BC break] Add native return type to `Translator` and to `Application::reset()`
81+
* Add `$block` argument to `AbstractController::renderView()`
8182
* Deprecate the integration of Doctrine annotations, either uninstall the `doctrine/annotations` package or disable
8283
the integration by setting `framework.annotations` to `false`
8384

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
6.4
55
---
66

7+
* Add `AbstractController::renderBlock()` and `$block` argument to `renderView()`
78
* Add native return type to `Translator` and to `Application::reset()`
89
* Deprecate the integration of Doctrine annotations, either uninstall the `doctrine/annotations` package or disable the integration by setting `framework.annotations` to `false`
910
* Enable `json_decode_detailed_errors` context for Serializer by default if `kernel.debug` is true and the `seld/jsonlint` package is installed

src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,13 @@ protected function denyAccessUnlessGranted(mixed $attribute, mixed $subject = nu
226226
* Returns a rendered view.
227227
*
228228
* Forms found in parameters are auto-cast to form views.
229+
*
230+
* @param string|null $block
229231
*/
230-
protected function renderView(string $view, array $parameters = []): string
232+
protected function renderView(string $view, array $parameters = []/* , string $block = null */): string
231233
{
234+
$block = 2 < \func_num_args() ? func_get_arg(2) : null;
235+
232236
if (!$this->container->has('twig')) {
233237
throw new \LogicException('You cannot use the "renderView" method if the Twig Bundle is not available. Try running "composer require symfony/twig-bundle".');
234238
}
@@ -239,6 +243,10 @@ protected function renderView(string $view, array $parameters = []): string
239243
}
240244
}
241245

246+
if (null !== $block) {
247+
return $this->container->get('twig')->load($view)->renderBlock($block, $parameters);
248+
}
249+
242250
return $this->container->get('twig')->render($view, $parameters);
243251
}
244252

@@ -250,21 +258,18 @@ protected function renderView(string $view, array $parameters = []): string
250258
*/
251259
protected function render(string $view, array $parameters = [], Response $response = null): Response
252260
{
253-
$content = $this->renderView($view, $parameters);
254-
$response ??= new Response();
255-
256-
if (200 === $response->getStatusCode()) {
257-
foreach ($parameters as $v) {
258-
if ($v instanceof FormInterface && $v->isSubmitted() && !$v->isValid()) {
259-
$response->setStatusCode(422);
260-
break;
261-
}
262-
}
263-
}
264-
265-
$response->setContent($content);
261+
return $this->doRender($view, null, $parameters, $response);
262+
}
266263

267-
return $response;
264+
/**
265+
* Renders a block in a view.
266+
*
267+
* If an invalid form is found in the list of parameters, a 422 status code is returned.
268+
* Forms found in parameters are auto-cast to form views.
269+
*/
270+
protected function renderBlock(string $view, string $block, array $parameters = [], Response $response = null): Response
271+
{
272+
return $this->doRender($view, $block, $parameters, $response);
268273
}
269274

270275
/**
@@ -431,4 +436,23 @@ protected function sendEarlyHints(iterable $links = [], Response $response = nul
431436

432437
return $response;
433438
}
439+
440+
private function doRender(string $view, ?string $block, array $parameters, ?Response $response): Response
441+
{
442+
$content = $this->renderView($view, $parameters, $block);
443+
$response ??= new Response();
444+
445+
if (200 === $response->getStatusCode()) {
446+
foreach ($parameters as $v) {
447+
if ($v instanceof FormInterface && $v->isSubmitted() && !$v->isValid()) {
448+
$response->setStatusCode(422);
449+
break;
450+
}
451+
}
452+
}
453+
454+
$response->setContent($content);
455+
456+
return $response;
457+
}
434458
}

0 commit comments

Comments
 (0)