Fix the possibility to override an aliased block (via use)

This commit is contained in:
Fabien Potencier
2024-09-25 22:07:53 +02:00
parent 3ddab4f8b4
commit 955611d5bb
4 changed files with 29 additions and 2 deletions
+1
View File
@@ -1,5 +1,6 @@
# 3.15.0 (2024-XX-XX)
* Fix the possibility to override an aliased block (via use)
* Add template cache hot reload
* Allow Twig callable argument names to be free-form (snake-case or camelCase) independently of the PHP callable signature
They were automatically converted to snake-cased before
+5 -1
View File
@@ -244,7 +244,11 @@ final class ModuleNode extends Node
->string($key)
->raw(\sprintf(']; unset($_trait_%s_blocks[', $i))
->string($key)
->raw("]);\n\n")
->raw("]); \$this->traitAliases[")
->subcompile($value)
->raw("] = ")
->string($key)
->raw(";\n\n")
;
}
}
+2 -1
View File
@@ -37,6 +37,7 @@ abstract class Template
protected $parents = [];
protected $blocks = [];
protected $traits = [];
protected $traitAliases = [];
protected $extensions = [];
protected $sandbox;
@@ -477,7 +478,7 @@ abstract class Template
public function yieldParentBlock($name, array $context, array $blocks = []): iterable
{
if (isset($this->traits[$name])) {
yield from $this->traits[$name][0]->yieldBlock($name, $context, $blocks, false);
yield from $this->traits[$name][0]->yieldBlock($this->traitAliases[$name] ?? $name, $context, $blocks, false);
} elseif ($parent = $this->getParent($context)) {
yield from $parent->unwrap()->yieldBlock($name, $context, $blocks, false);
} else {
@@ -0,0 +1,21 @@
--TEST--
"use" tag with an overridden block that is aliased
--TEMPLATE--
{% use "blocks.twig" with bar as baz %}
{% block foo %}{{ parent() }}+{% endblock %}
{% block baz %}{{ parent() }}+{% endblock %}
{{ block('foo') }}
{{ block('baz') }}
--TEMPLATE(blocks.twig)--
{% block foo %}Foo{% endblock %}
{% block bar %}Bar{% endblock %}
--DATA--
return []
--EXPECT--
Foo+
Bar+
Foo+
Bar+