Skip to content

Commit 9c22f7b

Browse files
Benjamin RICHARDfabpot
authored andcommitted
[FrameworkBundle] TemplateController should accept extra arguments to be sent to the template
1 parent 693390d commit 9c22f7b

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Added a new `mailer.message_bus` option to configure or disable the message bus to use to send mails.
99
* Added flex-compatible default implementations for `MicroKernelTrait::registerBundles()` and `getProjectDir()`
1010
* Deprecated passing a `RouteCollectionBuiler` to `MicroKernelTrait::configureRoutes()`, type-hint `RoutingConfigurator` instead
11+
* The `TemplateController` now accepts context argument
1112

1213
5.0.0
1314
-----

Controller/TemplateController.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,19 @@ public function __construct(Environment $twig = null)
3333
/**
3434
* Renders a template.
3535
*
36-
* @param string $template The template name
37-
* @param int|null $maxAge Max age for client caching
38-
* @param int|null $sharedAge Max age for shared (proxy) caching
39-
* @param bool|null $private Whether or not caching should apply for client caches only
36+
* @param string $template The template name
37+
* @param int|null $maxAge Max age for client caching
38+
* @param int|null $sharedAge Max age for shared (proxy) caching
39+
* @param bool|null $private Whether or not caching should apply for client caches only
40+
* @param array $context The context (arguments) of the template
4041
*/
41-
public function templateAction(string $template, int $maxAge = null, int $sharedAge = null, bool $private = null): Response
42+
public function templateAction(string $template, int $maxAge = null, int $sharedAge = null, bool $private = null, array $context = []): Response
4243
{
4344
if (null === $this->twig) {
4445
throw new \LogicException('You can not use the TemplateController if the Twig Bundle is not available.');
4546
}
4647

47-
$response = new Response($this->twig->render($template));
48+
$response = new Response($this->twig->render($template, $context));
4849

4950
if ($maxAge) {
5051
$response->setMaxAge($maxAge);
@@ -63,8 +64,8 @@ public function templateAction(string $template, int $maxAge = null, int $shared
6364
return $response;
6465
}
6566

66-
public function __invoke(string $template, int $maxAge = null, int $sharedAge = null, bool $private = null): Response
67+
public function __invoke(string $template, int $maxAge = null, int $sharedAge = null, bool $private = null, array $context = []): Response
6768
{
68-
return $this->templateAction($template, $maxAge, $sharedAge, $private);
69+
return $this->templateAction($template, $maxAge, $sharedAge, $private, $context);
6970
}
7071
}

Tests/Controller/TemplateControllerTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Symfony\Bundle\FrameworkBundle\Controller\TemplateController;
1515
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
16+
use Twig\Environment;
17+
use Twig\Loader\ArrayLoader;
1618

1719
/**
1820
* @author Kévin Dunglas <[email protected]>
@@ -39,4 +41,22 @@ public function testNoTwig()
3941
$controller->templateAction('mytemplate')->getContent();
4042
$controller('mytemplate')->getContent();
4143
}
44+
45+
public function testContext()
46+
{
47+
$templateName = 'template_controller.html.twig';
48+
$context = [
49+
'param' => 'hello world',
50+
];
51+
$expected = '<h1>'.$context['param'].'</h1>';
52+
53+
$loader = new ArrayLoader();
54+
$loader->setTemplate($templateName, '<h1>{{param}}</h1>');
55+
56+
$twig = new Environment($loader);
57+
$controller = new TemplateController($twig);
58+
59+
$this->assertEquals($expected, $controller->templateAction($templateName, null, null, null, $context)->getContent());
60+
$this->assertEquals($expected, $controller($templateName, null, null, null, $context)->getContent());
61+
}
4262
}

0 commit comments

Comments
 (0)