Skip to content

[TwigBridge] Render a block via the #[Template] attribute #58028

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
Aug 19, 2024

Conversation

smnandre
Copy link
Member

Q A
Branch? 7.2
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #...
License MIT

The same way renderBlock was added in the AbstractController, and for the same reasons, this PR adds a block parameter on the Template attribute, used by the TemplateAttributeListener.


Example taken from the documentation

class ProductController extends AbstractController
{
    #[Template('product/index.html.twig')]
    public function index(): array
    {
        // ...

        // when using the #[Template] attribute, you only need to return
        // an array with the parameters to pass to the template (the attribute
        // is the one which will create and return the Response object).
        return [
            'category' => '...',
            'promotions' => ['...', '...'],
        ];
    }
}

With this PR

class ProductController extends AbstractController
{
    #[Template('product/index.html.twig', block: 'main')]
    public function index(): array
    {
        return [
            'category' => '...',
            'promotions' => ['...', '...'],
        ];
    }
}

🛟 Only problem here, i don't get how to test this properly..

Twig\Template is abstract, Twig\TemplateWrapper is final, and Environment::load() must return a TemplateWrapper..

I looked at the AbstractController tests but i don't find one covering the load->displayBlock / load->renderBlock.

@stof
Copy link
Member

stof commented Aug 17, 2024

Make a test using an actual Twig instance, with a template that has a {% error %} tag in its top-level content (so that rendering the full template triggers an error). This way, you will be sure that it was not rendered in full.

@smnandre
Copy link
Member Author

Thank you @stof!

Make a test using an actual Twig instance, with a template that has a {% error %} tag in its top-level content (so that rendering the full template triggers an error). This way, you will be sure that it was not rendered in full.

had to remove the error tag (kept some ERROR text)... as the parsing of the tag made compilation fail.

@stof
Copy link
Member

stof commented Aug 17, 2024

ah sorry. We don't have the {% error %} in core, only the {% deprecated %} tag. I confused them.

@smnandre
Copy link
Member Author

Thank you for the help ;)

@fabpot fabpot force-pushed the feat/template-attribute-block branch from 18264ea to 14d4316 Compare August 19, 2024 07:23
@fabpot
Copy link
Member

fabpot commented Aug 19, 2024

Thank you @smnandre.

@fabpot fabpot merged commit 3c6f465 into symfony:7.2 Aug 19, 2024
1 check was pending
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Aug 26, 2024
…ate]` attribute (smnandre)

This PR was squashed before being merged into the 7.2 branch.

Discussion
----------

[Templating] [Template] Render a block with the `#[Template]` attribute

Document  [symfony/symfony#58028](symfony/symfony#58028)
and closes #20123

Commits
-------

d883d56 [Templating] [Template] Render a block with the `#[Template]` attribute
@fabpot fabpot mentioned this pull request Oct 27, 2024
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.

5 participants