diff --git a/CHANGELOG b/CHANGELOG index 7117de127..a554fa4b2 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 2.6.1 (2018-XX-XX) + * fixed error location when calling an undefined block * deprecated passing a string as a source on Twig_Error * switched generated code to use the PHP short array notation diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 98b1dfdaf..634e49d84 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -166,7 +166,7 @@ abstract class Twig_Template * * @internal */ - public function displayBlock($name, array $context, array $blocks = [], $useBlocks = true) + public function displayBlock($name, array $context, array $blocks = [], $useBlocks = true, self $templateContext = null) { if ($useBlocks && isset($blocks[$name])) { $template = $blocks[$name][0]; @@ -204,11 +204,11 @@ abstract class Twig_Template throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, $template->getSourceContext(), $e); } } elseif (false !== $parent = $this->getParent($context)) { - $parent->displayBlock($name, $context, array_merge($this->blocks, $blocks), false); + $parent->displayBlock($name, $context, array_merge($this->blocks, $blocks), false, $templateContext ?? $this); } elseif (isset($blocks[$name])) { throw new Twig_Error_Runtime(sprintf('Block "%s" should not call parent() in "%s" as the block does not exist in the parent template "%s".', $name, $blocks[$name][0]->getTemplateName(), $this->getTemplateName()), -1, $blocks[$name][0]->getSourceContext()); } else { - throw new Twig_Error_Runtime(sprintf('Block "%s" on template "%s" does not exist.', $name, $this->getTemplateName()), -1, $this->getSourceContext()); + throw new Twig_Error_Runtime(sprintf('Block "%s" on template "%s" does not exist.', $name, $this->getTemplateName()), -1, ($templateContext ?? $this)->getSourceContext()); } } diff --git a/test/Twig/Tests/Fixtures/functions/undefined_block.test b/test/Twig/Tests/Fixtures/functions/undefined_block.test index e7964f933..84b3eef61 100644 --- a/test/Twig/Tests/Fixtures/functions/undefined_block.test +++ b/test/Twig/Tests/Fixtures/functions/undefined_block.test @@ -2,11 +2,15 @@ "block" function with undefined block --TEMPLATE-- {% extends "base.twig" %} -{% block foo %}{{ parent() }}{{ block('unknown') }}{{ block('bar') }}{% endblock %} +{% block foo %} + {{ parent() }} + {{ block('unknown') }} + {{ block('bar') }} +{% endblock %} --TEMPLATE(base.twig)-- {% block foo %}Foo{% endblock %} {% block bar %}Bar{% endblock %} --DATA-- return [] --EXCEPTION-- -Twig_Error_Runtime: Block "unknown" on template "base.twig" does not exist in "base.twig" at line 2. +Twig_Error_Runtime: Block "unknown" on template "base.twig" does not exist in "index.twig" at line 5. diff --git a/test/Twig/Tests/Fixtures/functions/undefined_block_deep.test b/test/Twig/Tests/Fixtures/functions/undefined_block_deep.test new file mode 100644 index 000000000..f0a6f8693 --- /dev/null +++ b/test/Twig/Tests/Fixtures/functions/undefined_block_deep.test @@ -0,0 +1,20 @@ +--TEST-- +"block" function with undefined block on deep inheritance +--TEMPLATE-- +{% extends "base.twig" %} +{% block foo %} + {{ parent() }} + {{ block('unknown') }} + {{ block('bar') }} +{% endblock %} +--TEMPLATE(base.twig)-- +{% extends "layout.twig" %} +{% block foo %}Foo{% endblock %} +{% block bar %}Bar{% endblock %} +--TEMPLATE(layout.twig)-- +{% block foo %}Foo{% endblock %} +{% block bar %}Bar{% endblock %} +--DATA-- +return [] +--EXCEPTION-- +Twig_Error_Runtime: Block "unknown" on template "layout.twig" does not exist in "index.twig" at line 5.