Skip to content

Commit cbc337d

Browse files
committed
feature #35257 [FrameworkBundle] TemplateController should accept extra arguments to be sent to the template (Benjamin RICHARD)
This PR was squashed before being merged into the 5.1-dev branch (closes #35257). Discussion ---------- [FrameworkBundle] TemplateController should accept extra arguments to be sent to the template | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #... <!-- prefix each issue number with "Fix #", if any --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/roadmap): - Always add tests and ensure they pass. - Never break backward compatibility (see https://symfony.com/bc). - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too.) - Features and deprecations must be submitted against branch master. --> In the official documentation (symfony.com/doc/master/templates.html#rendering-a-template-directly-from-a-route) it says that TemplateController should accept extra arguments. In fact it's not available for instance. So i added the context argument as an array. Because of deprecation of templating, only the twig instance will apply the context argument. It will need to be implemented in branch 5. The following issue has been created in documentation project : symfony/symfony-docs#12897 Commits ------- e27b417817 [FrameworkBundle] TemplateController should accept extra arguments to be sent to the template
2 parents e71ee6c + 9c22f7b commit cbc337d

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)