Skip to content

Commit

Permalink
bug #2415 Add error message when calling parent() in a block that doe…
Browse files Browse the repository at this point in the history
…sn't exist (pierredup)

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

Discussion
----------

Add error message when calling parent() in a block that doesn't exist

Related: #2309

When calling `parent()` in a block that isn't defined in the parent template, you get an error message that states `Block "%s" on template "%s" does not exist`, even if the block is wrapped in an `is defined` check, which is confusing and makes debugging hard.

This fix adds a better error message indicating that you should not call `parent()` when the parent block is not defined.

Before:
![screen shot 2017-03-08 at 08 08 38](https://cloud.githubusercontent.com/assets/144858/23693410/0e476242-03dd-11e7-9621-4442bdc6bf05.png)

After:
![screen shot 2017-03-08 at 08 56 18](https://cloud.githubusercontent.com/assets/144858/23693424/234d9c2e-03dd-11e7-8d07-4e277d80f7e4.png)

Commits
-------

4587f67 Add error message when calling parent() in a block that doesn't exist
  • Loading branch information
fabpot committed Mar 10, 2017
2 parents 82004b8 + 4587f67 commit 261c4ec
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG
@@ -1,6 +1,6 @@
* 2.2.1 (2017-XX-XX)

* n/a
* added error message when calling `parent()` in a block that doesn't exist in the parent template

* 2.2.0 (2017-02-26)

Expand Down
2 changes: 2 additions & 0 deletions lib/Twig/Template.php
Expand Up @@ -204,6 +204,8 @@ public function displayBlock($name, array $context, array $blocks = array(), $us
}
} elseif (false !== $parent = $this->getParent($context)) {
$parent->displayBlock($name, $context, array_merge($this->blocks, $blocks), false);
} 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]->getTemplateName());
} else {
throw new Twig_Error_Runtime(sprintf('Block "%s" on template "%s" does not exist.', $name, $this->getTemplateName()), -1, $this->getTemplateName());
}
Expand Down
11 changes: 11 additions & 0 deletions test/Twig/Tests/Fixtures/functions/block_without_parent.test
@@ -0,0 +1,11 @@
--TEST--
"block" calling parent() with no definition in parent template
--TEMPLATE--
{% extends "parent.twig" %}
{% block label %}{{ parent() }}{% endblock %}
--TEMPLATE(parent.twig)--
{{ block('label') }}
--DATA--
return array()
--EXCEPTION--
Twig_Error_Runtime: Block "label" should not call parent() in "index.twig" as the block does not exist in the parent template "parent.twig" in "index.twig" at line 3.
11 changes: 11 additions & 0 deletions test/Twig/Tests/TemplateTest.php
Expand Up @@ -131,6 +131,17 @@ public function testDisplayBlockWithUndefinedBlock()
$template->displayBlock('unknown', array());
}

/**
* @expectedException Twig_Error_Runtime
* @expectedExceptionMessage Block "foo" should not call parent() in "index.twig" as the block does not exist in the parent template "parent.twig"
*/
public function testDisplayBlockWithUndefinedParentBlock()
{
$twig = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock());
$template = new Twig_TemplateTest($twig, 'parent.twig');
$template->displayBlock('foo', array(), array('foo' => array(new Twig_TemplateTest($twig, 'index.twig'), 'block_foo')), false);
}

public function testGetAttributeOnArrayWithConfusableKey()
{
$twig = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock());
Expand Down

0 comments on commit 261c4ec

Please sign in to comment.