-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] Add AbstractController::renderBlock()
and renderBlockView()
#51327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
I think it might be more readable with a separate |
In order to prevent code duplication, we could add renderBlock but keep the new argument on renderView. Otherwise we'll need a new renderBlockView and it should duplicate most of the code in renderView/render. I'm not keen on adding renderBlockView. |
Mmm, even adding renderBlock requires duplicating from render. Another idea might be to use the parameters array to add options, for example With all this in mind, I think I prefer the additional argument. |
I prefer the separate methods approach too, as it is more aligned with SRP and also results in a better argument order. return $this->renderBlock('foo.html.twig', 'the_block', $context); An alternative could be moving the duplicated code to new private methods
private function normalizeViewParameters(array $parame
8000
ters): array
{
foreach ($parameters as $k => $v) {
if ($v instanceof FormInterface) {
$parameters[$k] = $v->createView();
}
}
return $parameters;
}
private function prepareResponse(Response $response, array $parameters, string $content): Response
{
if (200 === $response->getStatusCode()) {
foreach ($parameters as $v) {
if ($v instanceof FormInterface && $v->isSubmitted() && !$v->isValid()) {
$response->setStatusCode(422);
break;
}
}
}
$response->setContent($content);
return $response;
} |
$block
argument to AbstractController:render()
and renderView()
AbstractController::renderBlock()
and $block
argument to renderView()
cea3081
to
29a7ae9
Compare
OK, thanks for the feedback :) PR updated to add method |
by the way, I would do the same for |
Full suggestion https://gist.github.com/yceruto/fc5594963df6a642c34111eea1a82b17 |
I'm not sold on renderViewBlock. There I still like the additional argument for renderView... |
Friendly ping @symfony/mergers |
src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php
Outdated
Show resolved
Hide resolved
AbstractController::renderBlock()
and $block
argument to renderView()
AbstractController::renderBlock()
and renderBlockView()
29a7ae9
to
cfc7ac2
Compare
cfc7ac2
to
fbf3814
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it !
…reams (nicolas-grekas) This PR was merged into the 2.x branch. Discussion ---------- [Turbo] Use blocks instead of partials to render turbo-streams | Q | A | ------------- | --- | Bug fix? | no | New feature? | | Tickets | - | License | MIT Relies on symfony/symfony#51327 for the call to `renderBlock()`. Using `targets` with a CSS selector empowers users with useful knowledge IMHO. Commits ------- e8a68ad [Turbo] Use blocks instead of partials to render turbo-streams
This would be especially useful when generating turbo-stream responses.
Right now, the doc recommends creating new partial templates, but this increases the complexity and scatters HTML fragments. Instead, we could encourage using twig blocks.
Adding this could help remove some boilerplate.
before (using blocks):
after: