From 23f53d596881dad2b437ce6d9a72c5d8d0d183b6 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sat, 24 Sep 2016 12:08:36 +0100 Subject: [PATCH] Improved the error message when a child template defines contents outside parent blocks --- lib/Twig/Parser.php | 4 ++-- .../exceptions/child_contents_outside_blocks.test | 15 +++++++++++++++ test/Twig/Tests/ParserTest.php | 2 +- 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/exceptions/child_contents_outside_blocks.test diff --git a/lib/Twig/Parser.php b/lib/Twig/Parser.php index 86bd494f03..62a2cea572 100644 --- a/lib/Twig/Parser.php +++ b/lib/Twig/Parser.php @@ -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 diff --git a/test/Twig/Tests/Fixtures/exceptions/child_contents_outside_blocks.test b/test/Twig/Tests/Fixtures/exceptions/child_contents_outside_blocks.test new file mode 100644 index 0000000000..8f94aa30a2 --- /dev/null +++ b/test/Twig/Tests/Fixtures/exceptions/child_contents_outside_blocks.test @@ -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. diff --git a/test/Twig/Tests/ParserTest.php b/test/Twig/Tests/ParserTest.php index 8f6831b972..afea63d541 100644 --- a/test/Twig/Tests/ParserTest.php +++ b/test/Twig/Tests/ParserTest.php @@ -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() {