Skip to content

Commit 38a7dfb

Browse files
committed
bug #40373 Check if templating engine supports given view (fritzmg)
This PR was merged into the 4.4 branch. Discussion ---------- Check if templating engine supports given view | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Currently the `ControllerTrait::render` and `ControllerTrait::renderView` methods assume that when the `templating` service is available it can always render the given view via that service. However, that might not be the case. For example if `framework.templating` is not configured to support the `twig` engine for example but only some other custom engine, e.g. ```yaml framework: templating: engines: - foobar ``` it will fail when the given view references a Twig template for instance and the configured engine cannot render that. The `Symfony\Component\Templating\EngineInterface` which `templating` implements defines a `support` method: https://github.com/symfony/symfony/blob/2536e178b183aeece7ac1e8e9195d6004025f25e/src/Symfony/Component/Templating/EngineInterface.php#L56-L63 which should be used here, in order to make sure that `templating` actually does support rendering the given view. Commits ------- 0f9434c189 check if templating engine supports view
2 parents 36481ae + 1871da9 commit 38a7dfb

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

Controller/ControllerTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ protected function denyAccessUnlessGranted($attributes, $subject = null, string
207207
*/
208208
protected function renderView(string $view, array $parameters = []): string
209209
{
210-
if ($this->container->has('templating')) {
210+
if ($this->container->has('templating') && $this->container->get('templating')->supports($view)) {
211211
@trigger_error('Using the "templating" service is deprecated since version 4.3 and will be removed in 5.0; use Twig instead.', \E_USER_DEPRECATED);
212212

213213
return $this->container->get('templating')->render($view, $parameters);
@@ -227,7 +227,7 @@ protected function renderView(string $view, array $parameters = []): string
227227
*/
228228
protected function render(string $view, array $parameters = [], Response $response = null): Response
229229
{
230-
if ($this->container->has('templating')) {
230+
if ($this->container->has('templating') && $this->container->get('templating')->supports($view)) {
231231
@trigger_error('Using the "templating" service is deprecated since version 4.3 and will be removed in 5.0; use Twig instead.', \E_USER_DEPRECATED);
232232

233233
$content = $this->container->get('templating')->render($view, $parameters);

Tests/Controller/ControllerTraitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ public function testRenderViewTemplating()
449449
{
450450
$templating = $this->createMock(EngineInterface::class);
451451
$templating->expects($this->once())->method('render')->willReturn('bar');
452+
$templating->expects($this->once())->method('supports')->willReturn(true);
452453

453454
$container = new Container();
454455
$container->set('templating', $templating);
@@ -466,6 +467,7 @@ public function testRenderTemplating()
466467
{
467468
$templating = $this->createMock(EngineInterface::class);
468469
$templating->expects($this->once())->method('render')->willReturn('bar');
470+
$templating->expects($this->once())->method('supports')->willReturn(true);
469471

470472
$container = new Container();
471473
$container->set('templating', $templating);

0 commit comments

Comments
 (0)