From 650295f59298b09f8471e1dcef0ee124440f6495 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 17 Oct 2012 19:24:30 +0200 Subject: [PATCH] fixed wrong template location in error messages (closes #803) --- lib/Twig/Error.php | 4 +++- lib/Twig/Template.php | 2 +- test/Twig/Tests/ErrorTest.php | 23 +++++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/Twig/Error.php b/lib/Twig/Error.php index b5ac00e..977b7eb 100644 --- a/lib/Twig/Error.php +++ b/lib/Twig/Error.php @@ -155,7 +155,9 @@ class Twig_Error extends Exception $template = null; foreach (debug_backtrace() as $trace) { if (isset($trace['object']) && $trace['object'] instanceof Twig_Template && 'Twig_Template' !== get_class($trace['object'])) { - $template = $trace['object']; + if (null === $this->filename || $this->filename == $trace['object']->getTemplateName()) { + $template = $trace['object']; + } } } diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 64a08f6..5a9d53c 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -304,7 +304,7 @@ abstract class Twig_Template implements Twig_TemplateInterface return null; } - throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $item)); + throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $item), -1, $this->getTemplateName()); } return $context[$item]; diff --git a/test/Twig/Tests/ErrorTest.php b/test/Twig/Tests/ErrorTest.php index 9d20c4d..9b28697 100644 --- a/test/Twig/Tests/ErrorTest.php +++ b/test/Twig/Tests/ErrorTest.php @@ -99,6 +99,29 @@ class Twig_Tests_ErrorTest extends PHPUnit_Framework_TestCase } } + public function testTwigExceptionAddsFileAndLineWhenMissingWithInheritanceAgain() + { + $loader = new Twig_Loader_Array(array( + 'index' => "{% extends 'base' %} + {% block content %} + {{ parent() }} + {% endblock %}", + 'base' => '{% block content %}{{ foo }}{% endblock %}' + )); + $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false)); + + $template = $twig->loadTemplate('index'); + try { + $template->render(array()); + + $this->fail(); + } catch (Twig_Error_Runtime $e) { + $this->assertEquals('Variable "foo" does not exist in "base" at line 1', $e->getMessage()); + $this->assertEquals(1, $e->getTemplateLine()); + $this->assertEquals('base', $e->getTemplateFile()); + } + } + public function testTwigExceptionAddsFileAndLineWhenMissingWithInheritanceOnDisk() { $loader = new Twig_Loader_Filesystem(dirname(__FILE__).'/Fixtures/errors'); -- 1.7.2.5