Skip to content

Commit

Permalink
minor #2139 Improved the error message when a child template defines …
Browse files Browse the repository at this point in the history
…contents outside parent blocks (javiereguiluz)

This PR was squashed before being merged into the 1.x branch (closes #2139).

Discussion
----------

Improved the error message when a child template defines contents outside parent blocks

### Context

When using template inheritance, you cannot put template contents outside Twig blocks (except some special tags (form_theme), macros, etc.) Sadly, making this mistake is very common when learning Twig.

### Problem

The error message associated to this issue is cryptic and "impossible to understand" for newcomers:

```
A template that extends another one cannot have a body.
```

I recently delivered a Symfony training and nobody was able to understand what the error was. They didn't have the slightest clue. I explained it to them ... and a few days latter they had the same problem and again they couldn't remember what this error was related to.

### Solution

Most of the Twig/Symfony errors are very helpful. Let's improve this one to make it useful too.

Two years ago I tried to fix this error message (see #1396). Let's see if this new PR is accepted.

Commits
-------

23f53d5 Improved the error message when a child template defines contents outside parent blocks
  • Loading branch information
fabpot committed Sep 24, 2016
2 parents bba5e1a + 23f53d5 commit a80ec17
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/Twig/Parser.php
Expand Up @@ -373,10 +373,10 @@ protected function filterBodyNodes(Twig_NodeInterface $node)
(!$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference && $node instanceof Twig_NodeOutputInterface)
) {
if (false !== strpos((string) $node, chr(0xEF).chr(0xBB).chr(0xBF))) {
throw new Twig_Error_Syntax('A template that extends another one cannot have a body but a byte order mark (BOM) has been detected; it must be removed.', $node->getLine(), $this->getFilename());
throw new Twig_Error_Syntax('A template that extends another one cannot start with a byte order mark (BOM); it must be removed', $node->getLine(), $this->getFilename());
}

throw new Twig_Error_Syntax('A template that extends another one cannot have a body.', $node->getLine(), $this->getFilename());
throw new Twig_Error_Syntax('A template that extends another one cannot include contents outside Twig blocks. Did you forget to put the contents inside a {% block %} tag.', $node->getLine(), $this->getFilename());
}

// bypass "set" nodes as they "capture" the output
Expand Down
@@ -0,0 +1,15 @@
--TEST--
Exception for child templates defining contents outside blocks defined by parent
--TEMPLATE--
{% extends 'base.twig' %}

Content outside a block.

{% block sidebar %}
Content inside a block.
{% endblock %}
--TEMPLATE(base.twig)--
{% block sidebar %}
{% endblock %}
--EXCEPTION--
Twig_Error_Syntax: A template that extends another one cannot include contents outside Twig blocks. Did you forget to put the contents inside a {% block %} tag in "index.twig" at line 3.
2 changes: 1 addition & 1 deletion test/Twig/Tests/ParserTest.php
Expand Up @@ -100,7 +100,7 @@ public function getFilterBodyNodesDataThrowsException()

/**
* @expectedException Twig_Error_Syntax
* @expectedExceptionMessage A template that extends another one cannot have a body but a byte order mark (BOM) has been detected; it must be removed at line 1.
* @expectedExceptionMessage A template that extends another one cannot start with a byte order mark (BOM); it must be removed at line 1
*/
public function testFilterBodyNodesWithBOM()
{
Expand Down

0 comments on commit a80ec17

Please sign in to comment.