Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 3.31.2 (2026-XX-XX)

* n/a
* Fix blank text nested in tags outside of blocks in a child template being rendered

# 3.31.1 (2026-11-24)

Expand Down
27 changes: 27 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Twig\Node\MacrosNode;
use Twig\Node\ModuleNode;
use Twig\Node\Node;
use Twig\Node\NodeCaptureInterface;
use Twig\Node\NodeDocumentation;
use Twig\Node\Nodes;
use Twig\Node\PrintNode;
Expand Down Expand Up @@ -622,6 +623,12 @@ private function cleanupBodyForChildTemplates(Node $body): Node
if ($body instanceof TextNode && $body->isBlank()) {
return new EmptyNode();
}
if (!$body instanceof Nodes && Node::class !== $body::class) {
// a single root node (see CorrectnessNodeVisitor::getRootNodes())
$this->replaceNestedBlankTextNodes($body);

return $body;
}

foreach ($body as $k => $node) {
if ($node instanceof BlockReferenceNode) {
Expand All @@ -630,12 +637,32 @@ private function cleanupBodyForChildTemplates(Node $body): Node
} elseif ($node instanceof TextNode && $node->isBlank()) {
// remove nodes considered as "empty"
$body->removeNode($k);
} else {
$this->replaceNestedBlankTextNodes($node);
}
}

return $body;
}

/**
* Replaces (instead of removing) blank text to keep the structure of nodes like "if".
*/
private function replaceNestedBlankTextNodes(Node $node): void
{
if ($node instanceof NodeCaptureInterface || $node instanceof BlockReferenceNode) {
return;
}

foreach ($node as $k => $n) {
if ($n instanceof TextNode && $n->isBlank()) {
$node->setNode((string) $k, new EmptyNode($n->getTemplateLine()));
} else {
$this->replaceNestedBlankTextNodes($n);
}
}
}

private function checkPrecedenceDeprecations(ExpressionParserInterface $expressionParser, AbstractExpression $expr): void
{
$this->expressionRefs[$expr] = $expressionParser;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
"extends" tag does not output blank text nested in tags outside of blocks
--TEMPLATE--
[{{ include('child.twig') }}]
[{% embed 'layout.twig' %}
{% if true %}
{% set foo = 'embed' %}
{% endif %}
{% block content %}{{ foo }}{% endblock %}
{% endembed %}]
--TEMPLATE(child.twig)--
{% extends 'layout.twig' %}
{% for i in [1, 2] %}
{% if i %}
{% set unused = i %}
{% endif %}
{% endfor %}
{% with {a: 1} %}
{% set unused = 1 %}
{% endwith %}
{% if true %} {% endif %}
{% if true %}
{% set foo %} {{ 'bar' }} {% endset %}
{% endif %}
{% block content %}{{ foo }}{% endblock %}
--TEMPLATE(layout.twig)--
<!DOCTYPE html>{% block content %}{% endblock %}
--DATA--
return []
--EXPECT--
[
<!DOCTYPE html> bar ]
[
<!DOCTYPE html>embed]
39 changes: 39 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,29 @@ public function testCleanupBodyForChildTemplatesWithASingleNodeBody(): void
$this->assertInstanceOf(EmptyNode::class, $body);
}

public function testCleanupBodyForChildTemplatesReplacesNestedBlankText(): void
{
$twig = new Environment(new ArrayLoader());
$node = $twig->parse($twig->tokenize(new Source('{% extends "base" %}{% if true %} {% set foo %} {{ bar }}{% endset %}{% endif %}', 'index')));

$if = $node->getNode('body')->getNode('0')->getNode('1');
$this->assertInstanceOf(IfNode::class, $if);
$this->assertInstanceOf(EmptyNode::class, $if->getNode('tests')->getNode('1')->getNode('0'));
$this->assertInstanceOf(TextNode::class, $if->getNode('tests')->getNode('1')->getNode('1')->getNode('values')->getNode('body')->getNode('0'));
}

public function testCleanupBodyForChildTemplatesReplacesNestedBlankTextInASingleNodeBody(): void
{
$twig = new Environment(new ArrayLoader());
$twig->addTokenParser(new ParentSettingInConditionTokenParser());

$node = $twig->parse($twig->tokenize(new Source('{% set_parent_in_condition %}', 'index')));

$if = $node->getNode('body')->getNode('0');
$this->assertInstanceOf(IfNode::class, $if);
$this->assertInstanceOf(EmptyNode::class, $if->getNode('tests')->getNode('1'));
}

public function testCleanupBodyAcceptsAnUnregisteredBlockReference(): void
{
$twig = new Environment(new ArrayLoader());
Expand Down Expand Up @@ -771,3 +794,19 @@ public function getTag(): string
return 'set_parent';
}
}

class ParentSettingInConditionTokenParser extends AbstractTokenParser
{
public function parse(Token $token): Node
{
$this->parser->setParent(new ConstantExpression('base', $token->getLine()), false);
$this->parser->getStream()->expect(Token::BLOCK_END_TYPE);

return new IfNode(new Nodes([new ConstantExpression(true, $token->getLine()), new TextNode(' ', $token->getLine())]), null, $token->getLine());
}

public function getTag(): string
{
return 'set_parent_in_condition';
}
}
Loading