From dd1b1016a7ff01bdd80509cc0be1229d8bdb9574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Tue, 2 Jun 2015 11:19:07 +0200 Subject: [PATCH 1/2] Fixed error guessing for nested templates --- lib/Twig/Template.php | 9 ++++++++- .../exceptions/syntax_error_in_reused_template.test | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 test/Twig/Tests/Fixtures/exceptions/syntax_error_in_reused_template.test diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 1d54bb13a..86094e2da 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -255,7 +255,14 @@ abstract class Twig_Template implements Twig_TemplateInterface return $this->env->loadTemplate($template, $index); } catch (Twig_Error $e) { - $e->setTemplateFile($templateName ? $templateName : $this->getTemplateName()); + if (!$e->getTemplateFile()) { + $e->setTemplateFile($templateName ? $templateName : $this->getTemplateName()); + } + + if ($e->getTemplateLine()) { + throw $e; + } + if (!$line) { $e->guess(); } else { diff --git a/test/Twig/Tests/Fixtures/exceptions/syntax_error_in_reused_template.test b/test/Twig/Tests/Fixtures/exceptions/syntax_error_in_reused_template.test new file mode 100644 index 000000000..5dd9f3838 --- /dev/null +++ b/test/Twig/Tests/Fixtures/exceptions/syntax_error_in_reused_template.test @@ -0,0 +1,10 @@ +--TEST-- +Exception for syntax error in reused template +--TEMPLATE-- +{% use 'foo.twig' %} +--TEMPLATE(foo.twig)-- +{% block bar %} + {% do node.data = 5 %} +{% endblock %} +--EXCEPTION-- +Twig_Error_Syntax: Unexpected token "operator" of value "=" ("end of statement block" expected) in "foo.twig" at line 3 From 00fe940ebfc39ff89dceecd8f62c199cb17c9fe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Tue, 2 Jun 2015 11:21:27 +0200 Subject: [PATCH 2/2] Fixed CS --- lib/Twig/Template.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 86094e2da..0878d1640 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -75,7 +75,7 @@ abstract class Twig_Template implements Twig_TemplateInterface return false; } - if ($parent instanceof Twig_Template) { + if ($parent instanceof self) { return $this->parents[$parent->getTemplateName()] = $parent; } @@ -249,7 +249,7 @@ abstract class Twig_Template implements Twig_TemplateInterface return $this->env->resolveTemplate($template); } - if ($template instanceof Twig_Template) { + if ($template instanceof self) { return $template; } @@ -392,10 +392,10 @@ abstract class Twig_Template implements Twig_TemplateInterface * * @throws Twig_Error_Runtime if the attribute does not exist and Twig is running in strict mode and $isDefinedTest is false */ - protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_Template::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false) + protected function getAttribute($object, $item, array $arguments = array(), $type = self::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false) { // array - if (Twig_Template::METHOD_CALL !== $type) { + if (self::METHOD_CALL !== $type) { $arrayItem = is_bool($item) || is_float($item) ? (int) $item : $item; if ((is_array($object) && array_key_exists($arrayItem, $object)) @@ -408,7 +408,7 @@ abstract class Twig_Template implements Twig_TemplateInterface return $object[$arrayItem]; } - if (Twig_Template::ARRAY_CALL === $type || !is_object($object)) { + if (self::ARRAY_CALL === $type || !is_object($object)) { if ($isDefinedTest) { return false; } @@ -427,7 +427,7 @@ abstract class Twig_Template implements Twig_TemplateInterface } else { $message = sprintf('Key "%s" for array with keys "%s" does not exist', $arrayItem, implode(', ', array_keys($object))); } - } elseif (Twig_Template::ARRAY_CALL === $type) { + } elseif (self::ARRAY_CALL === $type) { if (null === $object) { $message = sprintf('Impossible to access a key ("%s") on a null variable', $item); } else { @@ -462,7 +462,7 @@ abstract class Twig_Template implements Twig_TemplateInterface } // object property - if (Twig_Template::METHOD_CALL !== $type) { + if (self::METHOD_CALL !== $type) { if (isset($object->$item) || array_key_exists((string) $item, $object)) { if ($isDefinedTest) { return true;