Fix for {% embed %} error line number bug
authorHenry Smith <henry@henrysmith.org>
Tue, 16 Jul 2013 21:08:51 +0000 (22:08 +0100)
committerFabien Potencier <fabien.potencier@gmail.com>
Sat, 3 Aug 2013 15:15:16 +0000 (17:15 +0200)
lib/Twig/Error.php
test/Twig/Tests/Fixtures/tags/embed/error_line.test [new file with mode: 0644]

index 72d91a9..61a4cfa 100644 (file)
@@ -186,6 +186,7 @@ class Twig_Error extends Exception
     protected function guessTemplateInfo()
     {
         $template = null;
+        $templateClass = null;
 
         if (version_compare(phpversion(), '5.3.6', '>=')) {
             $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT);
@@ -195,8 +196,11 @@ class Twig_Error extends Exception
 
         foreach ($backtrace as $trace) {
             if (isset($trace['object']) && $trace['object'] instanceof Twig_Template && 'Twig_Template' !== get_class($trace['object'])) {
-                if (null === $this->filename || $this->filename == $trace['object']->getTemplateName()) {
+                $currentClass = get_class($trace['object']);
+                $isEmbedContainer = 0 === strpos($templateClass, $currentClass);
+                if (null === $this->filename || ($this->filename == $trace['object']->getTemplateName() && !$isEmbedContainer)) {
                     $template = $trace['object'];
+                    $templateClass = get_class($trace['object']);
                 }
             }
         }
diff --git a/test/Twig/Tests/Fixtures/tags/embed/error_line.test b/test/Twig/Tests/Fixtures/tags/embed/error_line.test
new file mode 100644 (file)
index 0000000..71ab2e0
--- /dev/null
@@ -0,0 +1,16 @@
+--TEST--
+"embed" tag
+--TEMPLATE(index.twig)--
+FOO
+{% embed "foo.twig" %}
+    {% block c1 %}
+        {{ nothing }}
+    {% endblock %}
+{% endembed %}
+BAR
+--TEMPLATE(foo.twig)--
+{% block c1 %}{% endblock %}
+--DATA--
+return array()
+--EXCEPTION--
+Twig_Error_Runtime: Variable "nothing" does not exist in "index.twig" at line 5