Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
dd9a14e to3029c34CompareUh oh!
There was an error while loading.Please reload this page.
3029c34 to58bbb0aCompareUh oh!
There was an error while loading.Please reload this page.
src/Symfony/Bridge/Twig/Tests/EventListener/TemplateAttributeListenerTest.phpShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
58bbb0a tob0e9154CompareUh oh!
There was an error while loading.Please reload this page.
b0e9154 to98c1cfaCompareThere 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!
GromNaN commentedJul 11, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 useSensio\Bundle\FrameworkExtraBundle\Configuration\Template;class TemplateController/** * @Template */ publicfunctiontemplateAction(Request$request,string$template):array {$request->attributes->get('_template')->setTemplate($template);// Fetch datareturn [/* 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 OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
98c1cfa to5b19e69Comparenicolas-grekas commentedJul 12, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@GromNaN thanks trying and having a look. |
5b19e69 to1f14fa7Compare…urned by controllers
1f14fa7 toc252606CompareThanks 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/aAdd 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
@Templateannotation, 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
FormInterfaceinstances intoFormView, adjusting the status code to 422 when a non-valid form is found in the parameters, similarly to whatAbstractController::render()does.