-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[TwigBridge] Add #[Template()]
to describe how to render arrays returned by controllers
#46906
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
dd9a14e
to
3029c34
Compare
3029c34
to
58bbb0a
Compare
src/Symfony/Bridge/Twig/Tests/EventListener/TemplateAttributeListenerTest.php
Show resolved
Hide resolved
58bbb0a
to
b0e9154
Compare
b0e9154
to
98c1cfa
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.
LGTM!
I just tested on a project, it works for the general use-case 👏🏻. In a specific controller, I modify the name of the template, to set a value from the routing config (a kind of custom use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class TemplateController
/**
* @Template
*/
public function templateAction(Request $request, string $template): array
{
$request->attributes->get('_template')->setTemplate($template);
// Fetch data
return [/* some data for the template */];
}
} This cannot be done with the controller attributes. Even if I could access the |
src/Symfony/Bridge/Twig/EventListener/TemplateAttributeListener.php
Outdated
Show resolved
Hide resolved
98c1cfa
to
5b19e69
Compare
@GromNaN thanks trying and having a look. |
5b19e69
to
1f14fa7
Compare
…urned by controllers
1f14fa7
to
c252606
Compare
Thanks you @nicolas-grekas, that's a great solution. I would add a test to prevent regression #46914 The same feature would be useful on other attributes. |
Thanks for the PR.
That's already supported :) |
…istener (GromNaN) This PR was merged into the 6.2 branch. Discussion ---------- [TwigBridge] Add test on _template attribute for Twig listener | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | Fix #46906 (comment) | License | MIT | Doc PR | n/a Add test for the feature described in #46906 (comment) Commits ------- 86cf8de Test _template attribute for Twig listener
Extracted from #45415 (and modernized a lot).
Unlike the
@Template
annotation, this attribute mandates a template name as argument. This removes the need for any template-guesser logic, making things explicit.Using the attribute also turns
FormInterface
instances intoFormView
, adjusting the status code to 422 when a non-valid form is found in the parameters, similarly to whatAbstractController::render()
does.