diff --git a/src/Node/IfNode.php b/src/Node/IfNode.php index 8ba23ddb6..e74ca523b 100644 --- a/src/Node/IfNode.php +++ b/src/Node/IfNode.php @@ -50,8 +50,11 @@ class IfNode extends Node ->subcompile($this->getNode('tests')->getNode($i)) ->raw(") {\n") ->indent() - ->subcompile($this->getNode('tests')->getNode($i + 1)) ; + // The node might not exists if the content is empty + if ($this->getNode('tests')->hasNode($i + 1)) { + $compiler->subcompile($this->getNode('tests')->getNode($i + 1)); + } } if ($this->hasNode('else')) { diff --git a/tests/Fixtures/tags/if/empty_body.test b/tests/Fixtures/tags/if/empty_body.test new file mode 100644 index 000000000..ba49f6e1c --- /dev/null +++ b/tests/Fixtures/tags/if/empty_body.test @@ -0,0 +1,32 @@ +--TEST-- +empty "if" body in child template +--TEMPLATE-- +{% extends 'base.twig' %} + +{% set foo = '' %} + +{% if a is defined %} + +{% else %} + {% set foo = 'NOTHING' %} +{% endif %} + +{% if a is defined %} + +{% endif %} + +{% if a is defined %} + {% set foo = 'NOTHING' %} +{% else %} + +{% endif %} + +{% block content %} + {{ foo }} +{% endblock %} +--TEMPLATE(base.twig)-- +{% block content %}{% endblock %} +--DATA-- +return [] +--EXPECT-- + NOTHING