bug #4037 fix: #4033 add missing unwrap call on TemplateWrapper instance (TLG-Gildas)

This PR was merged into the 3.x branch.

Discussion
----------

fix: #4033 add missing unwrap call on TemplateWrapper instance

Commits
-------

6c631526 fix: #4033 add missing unwrap call when a TemplateWrapper instance can be present
This commit is contained in:
Fabien Potencier
2024-04-18 07:51:18 -04:00
3 changed files with 22 additions and 3 deletions
+2 -2
View File
@@ -198,7 +198,7 @@ final class ModuleNode extends Node
->raw(', ')
->repr($node->getTemplateLine())
->raw(");\n")
->write(sprintf("if (!\$_trait_%s->isTraitable()) {\n", $i))
->write(sprintf("if (!\$_trait_%s->unwrap()->isTraitable()) {\n", $i))
->indent()
->write("throw new RuntimeError('Template \"'.")
->subcompile($trait->getNode('template'))
@@ -207,7 +207,7 @@ final class ModuleNode extends Node
->raw(", \$this->source);\n")
->outdent()
->write("}\n")
->write(sprintf("\$_trait_%s_blocks = \$_trait_%s->getBlocks();\n\n", $i, $i))
->write(sprintf("\$_trait_%s_blocks = \$_trait_%s->unwrap()->getBlocks();\n\n", $i, $i))
;
foreach ($trait->getNode('targets') as $key => $value) {
+1 -1
View File
@@ -464,7 +464,7 @@ abstract class Template
}
}
} elseif ($parent = $this->getParent($context)) {
yield from $parent->yieldBlock($name, $context, array_merge($this->blocks, $blocks), false, $templateContext ?? $this);
yield from $parent->unwrap()->yieldBlock($name, $context, array_merge($this->blocks, $blocks), false, $templateContext ?? $this);
} elseif (isset($blocks[$name])) {
throw new RuntimeError(sprintf('Block "%s" should not call parent() in "%s" as the block does not exist in the parent template "%s".', $name, $blocks[$name][0]->getTemplateName(), $this->getTemplateName()), -1, $blocks[$name][0]->getSourceContext());
} else {
@@ -0,0 +1,19 @@
--TEST--
Call to undefined method Twig\\TemplateWrapper::yieldBlock()
--TEMPLATE--
{% extends 'parent' %}
{%- block content -%}
{{ parent() }}
child
{%- endblock -%}
--TEMPLATE(parent)--
{% extends ['unknowngrandparent', 'grandparent'] %}
--TEMPLATE(grandparent)--
{%- block content -%}
grandparent
{%- endblock -%}
--DATA--
return []
--EXPECT--
grandparent
child