tweaked the error message when using extends in a block/macro

This commit is contained in:
Fabien Potencier
2019-05-16 16:06:57 +02:00
parent 6315ea4786
commit 70c1b3a2c2
5 changed files with 25 additions and 11 deletions
+1 -1
View File
@@ -251,7 +251,7 @@ class Parser implements \Twig_ParserInterface
public function peekBlockStack()
{
return $this->blockStack[\count($this->blockStack) - 1];
return isset($this->blockStack[\count($this->blockStack) - 1]) ? $this->blockStack[\count($this->blockStack) - 1] : null;
}
public function popBlockStack()
+4 -2
View File
@@ -29,8 +29,10 @@ class ExtendsTokenParser extends AbstractTokenParser
{
$stream = $this->parser->getStream();
if (!$this->parser->isMainScope()) {
throw new SyntaxError('Cannot extend from a block.', $token->getLine(), $stream->getSourceContext());
if ($this->parser->peekBlockStack()) {
throw new SyntaxError('Cannot use "extend" in a block.', $token->getLine(), $stream->getSourceContext());
} elseif (!$this->parser->isMainScope()) {
throw new SyntaxError('Cannot use "extend" in a macro.', $token->getLine(), $stream->getSourceContext());
}
if (null !== $this->parser->getParent()) {
@@ -0,0 +1,10 @@
--TEST--
"extends" tag in a block
--TEMPLATE--
{% block foo %}
{% extends "foo.twig" %}
{% endblock %}
--DATA--
return []
--EXCEPTION--
Twig\Error\SyntaxError: Cannot use "extend" in a block in "index.twig" at line 3.
@@ -0,0 +1,10 @@
--TEST--
"extends" tag in a macro
--TEMPLATE--
{% macro foo() %}
{% extends "foo.twig" %}
{% endmacro %}
--DATA--
return []
--EXCEPTION--
Twig\Error\SyntaxError: Cannot use "extend" in a macro in "index.twig" at line 3.
@@ -1,8 +0,0 @@
--TEST--
"extends" tag
--TEMPLATE--
{% block content %}
{% extends "foo.twig" %}
{% endblock %}
--EXCEPTION--
Twig\Error\SyntaxError: Cannot extend from a block in "index.twig" at line 3.