Fix nested block() resolution when a directly rendered block calls parent()

This commit is contained in:
Fabien Potencier
2026-06-03 22:09:09 +02:00
parent a0093cd699
commit 279fe13b22
3 changed files with 31 additions and 0 deletions
+1
View File
@@ -1,5 +1,6 @@
# 3.27.2 (2026-XX-XX)
* Fix nested `block()` calls to resolve against the overriding template when a block rendered through `block(name, template)` calls `parent()`
* Stop reporting a skipped test in `IntegrationTestCase` when there is no legacy test to run
* Make the `IntegrationTestCase` and `NodeTestCase` test helpers compatible with PHPUnit 11
* Cast printed expressions to string so values that cannot be converted to a string (arrays, non-`Stringable` objects, ...) report a usable stack trace at the print location
+2
View File
@@ -440,6 +440,8 @@ abstract class Template
} elseif (isset($this->blocks[$name])) {
$template = $this->blocks[$name][0];
$block = $this->blocks[$name][1];
// expose this template's own blocks so nested block() calls resolve against them when the block is rendered directly (e.g. block(name, template))
$blocks = array_merge($this->blocks, $blocks);
} else {
$template = null;
$block = null;
@@ -0,0 +1,28 @@
--TEST--
"block" function with a template argument resolves nested "block" calls against the overriding template through parent()
--TEMPLATE--
{{ block('block1', 'theme.html.twig') }}
--TEMPLATE(theme.html.twig)--
{% extends 'base.html.twig' %}
{%- block block1 -%}
{{- parent() -}}
BLOCK1THEME/
{%- endblock -%}
{%- block block2 -%}
BLOCK2THEME/
{%- endblock -%}
--TEMPLATE(base.html.twig)--
{%- block block1 -%}
BLOCK1BASE/
{{- block('block2') -}}
{%- endblock -%}
{%- block block2 -%}
BLOCK2BASE/
{%- endblock -%}
--DATA--
return []
--EXPECT--
BLOCK1BASE/BLOCK2THEME/BLOCK1THEME/