Skip to content

[TwigBundle] FilesystemLoader::findTemplate() breaks FilesystemLoader::exists() with Twig3 #34877

@dpesch

Description

@dpesch

Symfony version(s) affected: 4.4.1 (with Twig >= 3.0)

Description
Symfony TwigBridge (4.4.1) FilesystemLoader extends \Twig\Loader\FilesystemLoader and overwrites findTemplate() (\Twig\Loader\FilesystemLoader::findTemplate).

With Twig3 the return values of the findTemplate() method has been changed and should return string|null (please see https://github.com/twigphp/Twig/blob/3.x/src/Loader/FilesystemLoader.php#L167).

But \Symfony\Bundle\TwigBundle\Loader\FilesystemLoader::findTemplate() returns false if the template does not exists and $throw is false (see

).

Twig3 \Twig\Loader\FilesystemLoader::exists() expects null for not found templates (please see https://github.com/twigphp/Twig/blob/a5f91f3d8fb2965be8b715323dbdc81ce288a14a/src/Loader/FilesystemLoader.php#L153) and returns true for not existing templates.

How to reproduce
Use templateExists() with Symfony TwigBridge 4.4.1 and Twig 3.0 or adapt the test \Symfony\Bundle\TwigBundle\Tests\Loader\FilesystemLoaderTest::testTwigSoftErrorIfTemplateDoesNotExist to the Twig3 declaration.

The test use $this->assertFalse(…) but should be changed to assertNull().

Possible Solution

I've prepared a pull request which fixes the test and the return value.

Twig 2 should not be affected. The exists() method expects null or false for not found templates: https://github.com/twigphp/Twig/blob/fdb691a424682a524555f73b2c665c06971c4ed5/src/Loader/FilesystemLoader.php#L169

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions