foreach (debug_backtrace() as $trace) {
if (isset($trace['object']) && $trace['object'] instanceof Twig_Template && 'Twig_Template' !== get_class($trace['object'])) {
$template = $trace['object'];
-
- // update template filename
- if (null === $this->filename) {
- $this->filename = $template->getTemplateName();
- }
-
- break;
}
}
+ // update template filename
+ if (null !== $template && null === $this->filename) {
+ $this->filename = $template->getTemplateName();
+ }
+
if (null === $template || $this->lineno > -1) {
return;
}
$twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
$template = $twig->loadTemplate('index');
-
try {
$template->render(array());
$this->assertEquals(3, $e->getTemplateLine());
$this->assertEquals('index', $e->getTemplateFile());
}
+
+ try {
+ $template->render(array('foo' => new Twig_Tests_ErrorTest_Foo()));
+
+ $this->fail();
+ } catch (Twig_Error_Runtime $e) {
+ $this->assertEquals('An exception has been thrown during the rendering of a template ("Runtime error...") in "index" at line 3.', $e->getMessage());
+ $this->assertEquals(3, $e->getTemplateLine());
+ $this->assertEquals('index', $e->getTemplateFile());
+ }
+ }
+
+ public function testTwigExceptionAddsFileAndLineWhenMissingWithInheritanceOnDisk()
+ {
+ $loader = new Twig_Loader_Filesystem(__DIR__.'/Fixtures/errors');
+ $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
+
+ $template = $twig->loadTemplate('index.html');
+ try {
+ $template->render(array());
+
+ $this->fail();
+ } catch (Twig_Error_Runtime $e) {
+ $this->assertEquals('Variable "foo" does not exist in "index.html" at line 3', $e->getMessage());
+ $this->assertEquals(3, $e->getTemplateLine());
+ $this->assertEquals('index.html', $e->getTemplateFile());
+ }
+
+ try {
+ $template->render(array('foo' => new Twig_Tests_ErrorTest_Foo()));
+
+ $this->fail();
+ } catch (Twig_Error_Runtime $e) {
+ $this->assertEquals('An exception has been thrown during the rendering of a template ("Runtime error...") in "index.html" at line 3.', $e->getMessage());
+ $this->assertEquals(3, $e->getTemplateLine());
+ $this->assertEquals('index.html', $e->getTemplateFile());
+ }
}
}