Skip to content

Commit 3e1a5e2

Browse files
committed
minor #16996 Add #[Template()] attribute (javiereguiluz)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- Add #[Template()] attribute Fixes #16969. Commits ------- 112e6bb Add #[Template()] attribute
2 parents f8d5eae + 112e6bb commit 3e1a5e2

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

best_practices.rst

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,6 @@ configuration. You don't need to browse several files created with different
234234
formats (YAML, XML, PHP): all the configuration is just where you need it and
235235
it only uses one format.
236236

237-
Don't Use Annotations to Configure the Controller Template
238-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
239-
240-
The ``@Template`` annotation is useful, but also involves some *magic*.
241-
Moreover, most of the time ``@Template`` is used without any parameters, which
242-
makes it more difficult to know which template is being rendered. It also hides
243-
the fact that a controller should always return a ``Response`` object.
244-
245237
Use Dependency Injection to Get Services
246238
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
247239

components/http_kernel.rst

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -409,12 +409,12 @@ return a ``Response``.
409409

410410
.. sidebar:: ``kernel.view`` in the Symfony Framework
411411

412-
There is no default listener inside the Symfony Framework for the ``kernel.view``
413-
event. However, `SensioFrameworkExtraBundle`_ *does* add a listener to this
414-
event. If your controller returns an array, and you place the `@Template`_
415-
annotation above the controller, then this listener renders a template,
416-
passes the array you returned from your controller to that template, and
417-
creates a ``Response`` containing the returned content from that template.
412+
There is a default listener inside the Symfony Framework for the ``kernel.view``
413+
event. If your controller action returns an array, and you apply the
414+
:ref:`#[Template()] attribute <templates-template-attribute>` to that
415+
controller action, then this listener renders a template, passes the array
416+
you returned from your controller to that template, and creates a ``Response``
417+
containing the returned content from that template.
418418

419419
Additionally, a popular community bundle `FOSRestBundle`_ implements
420420
a listener on this event which aims to give you a robust view layer
@@ -751,5 +751,4 @@ Learn more
751751
.. _`PHP FPM`: https://www.php.net/manual/en/install.fpm.php
752752
.. _`SensioFrameworkExtraBundle`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html
753753
.. _`@ParamConverter`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
754-
.. _`@Template`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/view.html
755754
.. _variadic: https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list

templates.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,38 @@ If your controller does not extend from ``AbstractController``, you'll need to
470470
:ref:`fetch services in your controller <controller-accessing-services>` and
471471
use the ``render()`` method of the ``twig`` service.
472472

473+
.. _templates-template-attribute:
474+
475+
Another option is to use the ``#[Template()]`` attribute on the controller method
476+
to define the template to render::
477+
478+
// src/Controller/ProductController.php
479+
namespace App\Controller;
480+
481+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
482+
use Symfony\Component\HttpFoundation\Response;
483+
484+
class ProductController extends AbstractController
485+
{
486+
#[Template('product/index.html.twig')]
487+
public function index()
488+
{
489+
// ...
490+
491+
// when using the #[Template()] attribute, you only need to return
492+
// an array with the parameters to pass to the template (the attribute
493+
// is the one which will create and return the Response object).
494+
return [
495+
'category' => '...',
496+
'promotions' => ['...', '...'],
497+
];
498+
}
499+
}
500+
501+
.. versionadded:: 6.2
502+
503+
The ``#[Template()]`` attribute was introduced in Symfony 6.2.
504+
473505
Rendering a Template in Services
474506
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
475507

0 commit comments

Comments
 (0)