diff --git a/CHANGELOG b/CHANGELOG index ada57c6b5..e64b0a2a9 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.37.0 (2019-XX-XX) + * fixed using a Twig_TemplateWrapper instance as an argument to extends * switched generated code to use the PHP short array notation * dropped PHP 5.3 support * fixed float representation in compiled templates diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 1008863de..3a96a1a3c 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -108,7 +108,7 @@ abstract class Twig_Template implements Twig_TemplateInterface * * @param array $context * - * @return Twig_TemplateInterface|false The parent template or false if there is no parent + * @return Twig_TemplateInterface|Twig_TemplateWrapper|false The parent template or false if there is no parent * * @internal */ @@ -125,8 +125,8 @@ abstract class Twig_Template implements Twig_TemplateInterface return false; } - if ($parent instanceof self) { - return $this->parents[$parent->getTemplateName()] = $parent; + if ($parent instanceof self || $parent instanceof Twig_TemplateWrapper) { + return $this->parents[$parent->getSourceContext()->getName()] = $parent; } if (!isset($this->parents[$parent])) { @@ -355,11 +355,7 @@ abstract class Twig_Template implements Twig_TemplateInterface return $this->env->resolveTemplate($template); } - if ($template instanceof self) { - return $template; - } - - if ($template instanceof Twig_TemplateWrapper) { + if ($template instanceof self || $template instanceof Twig_TemplateWrapper) { return $template; } diff --git a/lib/Twig/TemplateWrapper.php b/lib/Twig/TemplateWrapper.php index 18bbd20ed..b05d165c1 100644 --- a/lib/Twig/TemplateWrapper.php +++ b/lib/Twig/TemplateWrapper.php @@ -40,7 +40,9 @@ final class Twig_TemplateWrapper */ public function render($context = []) { - return $this->template->render($context); + // using func_get_args() allows to not expose the blocks argument + // as it should only be used by internal code + return $this->template->render($context, func_num_args() >= 1 ? func_get_arg(1) : []); } /** @@ -50,7 +52,9 @@ final class Twig_TemplateWrapper */ public function display($context = []) { - $this->template->display($context); + // using func_get_args() allows to not expose the blocks argument + // as it should only be used by internal code + $this->template->display($context, func_num_args() >= 1 ? func_get_arg(1) : []); } /** diff --git a/test/Twig/Tests/Fixtures/tags/inheritance/parent_as_template_wrapper.test b/test/Twig/Tests/Fixtures/tags/inheritance/parent_as_template_wrapper.test new file mode 100644 index 000000000..1aaed556c --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/inheritance/parent_as_template_wrapper.test @@ -0,0 +1,12 @@ +--TEST-- +"extends" tag with a parent as a Twig_TemplateWrapper instance +--TEMPLATE-- +{% extends foo %} + +{% block content %}New{% endblock %} +--TEMPLATE(foo.twig)-- +{% block content %}Default{% endblock %} +--DATA-- +return ['foo' => $twig->load('foo.twig')] +--EXPECT-- +New