Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Merged
merged 1 commit into from
Jul 12, 2022

Conversation

nicolas-grekas
Copy link
Member

Q A
Branch? 6.2
Bug fix? no
New feature? yes
Deprecations? no
Tickets Part of #44705
License MIT
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 turns FormInterface instances into FormView, adjusting the status code to 422 when a non-valid form is found in the parameters, similarly to what AbstractController::render() does.

Copy link
Member

@yceruto yceruto 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 commented Jul 11, 2022

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

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

@nicolas-grekas
Copy link
Member Author

nicolas-grekas commented Jul 12, 2022

@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
Copy link
Member

GromNaN commented Jul 12, 2022

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-grekas nicolas-grekas deleted the twig-template-attr branch July 12, 2022 09:26
@nicolas-grekas
Copy link
Member Author

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 :)

nicolas-grekas added a commit that referenced this pull request Jul 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/a

Add test for the feature described in #46906 (comment)

Commits
-------

86cf8de Test _template attribute for Twig listener
@fabpot fabpot mentioned this pull request Oct 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants