fixed error location when calling an undefined block

This commit is contained in:
Fabien Potencier
2019-01-12 03:50:49 +01:00
parent c27a2ff849
commit 92f36b7f38
4 changed files with 30 additions and 5 deletions
+1
View File
@@ -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
+3 -3
View File
@@ -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());
}
}
@@ -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.
@@ -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.