From e27b417817f79018b6172289e546a807e22f83ff Mon Sep 17 00:00:00 2001 From: Benjamin RICHARD Date: Wed, 8 Jan 2020 11:51:46 +0100 Subject: [PATCH] [FrameworkBundle] TemplateController should accept extra arguments to be sent to the template --- .../Bundle/FrameworkBundle/CHANGELOG.md | 1 + .../Controller/TemplateController.php | 17 ++++++++-------- .../Controller/TemplateControllerTest.php | 20 +++++++++++++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 7f1b87dc466b..3f1ce1ea32e3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -8,6 +8,7 @@ CHANGELOG * Added a new `mailer.message_bus` option to configure or disable the message bus to use to send mails. * Added flex-compatible default implementations for `MicroKernelTrait::registerBundles()` and `getProjectDir()` * Deprecated passing a `RouteCollectionBuiler` to `MicroKernelTrait::configureRoutes()`, type-hint `RoutingConfigurator` instead + * The `TemplateController` now accepts context argument 5.0.0 ----- diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php index 0fff40bac58e..f78f18877274 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php @@ -33,18 +33,19 @@ public function __construct(Environment $twig = null) /** * Renders a template. * - * @param string $template The template name - * @param int|null $maxAge Max age for client caching - * @param int|null $sharedAge Max age for shared (proxy) caching - * @param bool|null $private Whether or not caching should apply for client caches only + * @param string $template The template name + * @param int|null $maxAge Max age for client caching + * @param int|null $sharedAge Max age for shared (proxy) caching + * @param bool|null $private Whether or not caching should apply for client caches only + * @param array $context The context (arguments) of the template */ - public function templateAction(string $template, int $maxAge = null, int $sharedAge = null, bool $private = null): Response + public function templateAction(string $template, int $maxAge = null, int $sharedAge = null, bool $private = null, array $context = []): Response { if (null === $this->twig) { throw new \LogicException('You can not use the TemplateController if the Twig Bundle is not available.'); } - $response = new Response($this->twig->render($template)); + $response = new Response($this->twig->render($template, $context)); if ($maxAge) { $response->setMaxAge($maxAge); @@ -63,8 +64,8 @@ public function templateAction(string $template, int $maxAge = null, int $shared return $response; } - public function __invoke(string $template, int $maxAge = null, int $sharedAge = null, bool $private = null): Response + public function __invoke(string $template, int $maxAge = null, int $sharedAge = null, bool $private = null, array $context = []): Response { - return $this->templateAction($template, $maxAge, $sharedAge, $private); + return $this->templateAction($template, $maxAge, $sharedAge, $private, $context); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TemplateControllerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TemplateControllerTest.php index 452c9ffd0d78..60519e9bc05e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TemplateControllerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TemplateControllerTest.php @@ -13,6 +13,8 @@ use Symfony\Bundle\FrameworkBundle\Controller\TemplateController; use Symfony\Bundle\FrameworkBundle\Tests\TestCase; +use Twig\Environment; +use Twig\Loader\ArrayLoader; /** * @author Kévin Dunglas @@ -39,4 +41,22 @@ public function testNoTwig() $controller->templateAction('mytemplate')->getContent(); $controller('mytemplate')->getContent(); } + + public function testContext() + { + $templateName = 'template_controller.html.twig'; + $context = [ + 'param' => 'hello world', + ]; + $expected = '

'.$context['param'].'

'; + + $loader = new ArrayLoader(); + $loader->setTemplate($templateName, '

{{param}}

'); + + $twig = new Environment($loader); + $controller = new TemplateController($twig); + + $this->assertEquals($expected, $controller->templateAction($templateName, null, null, null, $context)->getContent()); + $this->assertEquals($expected, $controller($templateName, null, null, null, $context)->getContent()); + } }