From 03807fc73367c7d0b839635d6b0ff5dc8276d88a Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Fri, 12 Aug 2022 19:15:06 +0200 Subject: [PATCH] do not clean up whitespace text nodes inside if tags --- src/Node/IfNode.php | 5 ++++- tests/Fixtures/tags/if/empty_body.test | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 tests/Fixtures/tags/if/empty_body.test diff --git a/src/Node/IfNode.php b/src/Node/IfNode.php index 8ba23ddb60..e74ca523b0 100644 --- a/src/Node/IfNode.php +++ b/src/Node/IfNode.php @@ -50,8 +50,11 @@ public function compile(Compiler $compiler) ->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 0000000000..9b11ae033b --- /dev/null +++ b/tests/Fixtures/tags/if/empty_body.test @@ -0,0 +1,26 @@ +--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 %} + +{% block content %} + {{ foo }} +{% endblock %} +--TEMPLATE(base.twig)-- +{% block content %}{% endblock %} +--DATA-- +return [] +--EXPECT-- + NOTHING