Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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

Merged

Conversation

@nicolas-grekas
Copy link
Member

QA
Branch?6.2
Bug fix?no
New feature?yes
Deprecations?no
TicketsPart of#44705
LicenseMIT
Doc PR-

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 turnsFormInterface instances intoFormView, adjusting the status code to 422 when a non-valid form is found in the parameters, similarly to whatAbstractController::render() does.

Copy link
Member

@ycerutoyceruto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

LGTM!

@GromNaN
Copy link
Member

GromNaN commentedJul 11, 2022
edited
Loading

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 customTemplateController). The template is not rendered in the controller because there is some processing on the data between the controller result and the template listener.

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 theControllerArgumentsEvent or theControllerEvent, they provide access to the attributes, but do not allow update.

@nicolas-grekas
Copy link
MemberAuthor

nicolas-grekas commentedJul 12, 2022
edited
Loading

@GromNaN thanks trying and having a look.
The code now reads the_template attribute on the request. The attribute is not pre-populated because the listener reads it only after the controller is called, this means you can now do:
$request->attributes->set('_template', new Template($template))

GromNaN and apfelbox reacted with hooray emoji

@GromNaN
Copy link
Member

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.

@nicolas-grekasnicolas-grekas deleted the twig-template-attr branchJuly 12, 2022 09:26
@nicolas-grekas
Copy link
MemberAuthor

I would add a test to prevent regression#46914

Thanks for the PR.

The same feature would be useful on other attributes.

That's already supported :)

GromNaN reacted with thumbs up emoji

nicolas-grekas added a commit that referenced this pull requestJul 12, 2022
…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
@fabpotfabpot mentioned this pull requestOct 24, 2022
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@kbondkbondkbond approved these changes

@GromNaNGromNaNGromNaN approved these changes

@ycerutoycerutoyceruto approved these changes

@wouterjwouterjAwaiting requested review from wouterjwouterj is a code owner

@chalasrchalasrAwaiting requested review from chalasrchalasr is a code owner

+2 more reviewers

@ro0NLro0NLro0NL left review comments

@HeahDudeHeahDudeHeahDude approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

6.2

Development

Successfully merging this pull request may close these issues.

7 participants

@nicolas-grekas@GromNaN@kbond@ro0NL@yceruto@HeahDude@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp