Skip to content

Commit

Permalink
made the filesystem loader compatible with Twig 2.0
Browse files Browse the repository at this point in the history
Without adding the exists() method, the code happens to work by chance,
just because the current implementation of Twig exits() method calls
findTemplate().

But we know that it won't be the case anymore as of Twig 2.0.
  • Loading branch information
fabpot committed Aug 8, 2013
1 parent ec16d89 commit 0b965fb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php
Expand Up @@ -40,6 +40,25 @@ public function __construct(FileLocatorInterface $locator, TemplateNameParserInt
$this->cache = array();
}

/**
* {@inheritdoc}
*/
public function exists($template)
{
if (parent::exists($template)) {
return true;
}

// same logic as findTemplate below for the fallback
try {
$this->cache[(string) $template] = $this->locator->locate($this->parser->parse($template));
} catch (\Exception $e) {
return false;
}

return true;
}

/**
* Returns the path to the template file.
*
Expand Down
Expand Up @@ -38,6 +38,21 @@ public function testGetSource()
$this->assertEquals("This is a layout\n", $loader->getSource('TwigBundle::layout.html.twig'));
}

public function testExists()
{
// should return true for templates that Twig does not find, but Symfony does
$parser = $this->getMock('Symfony\Component\Templating\TemplateNameParserInterface');
$locator = $this->getMock('Symfony\Component\Config\FileLocatorInterface');
$locator
->expects($this->once())
->method('locate')
->will($this->returnValue($template = __DIR__.'/../DependencyInjection/Fixtures/Resources/views/layout.html.twig'))
;
$loader = new FilesystemLoader($locator, $parser);

return $this->assertTrue($loader->exists($template));
}

/**
* @expectedException Twig_Error_Loader
*/
Expand Down

0 comments on commit 0b965fb

Please sign in to comment.