fixed macro inheritance

This commit is contained in:
Fabien Potencier
2019-06-03 11:10:20 +02:00
parent 23fb803e5b
commit 21ecf010a3
4 changed files with 32 additions and 6 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
* 2.11.1 (2019-XX-XX)
* n/a
* fixed support for macros defined in parent templates
* 2.11.0 (2019-05-31)
+10 -4
View File
@@ -1080,13 +1080,19 @@ function twig_capitalize_string_filter(Environment $env, $string)
/**
* @internal
*/
function twig_call_macro($object, string $method, array $args, int $lineno, Source $source)
function twig_call_macro(Template $template, string $method, array $args, int $lineno, array $context, Source $source)
{
if (!method_exists($object, $method)) {
throw new RuntimeError(sprintf('Macro "%s" is not defined in template "%s".', substr($method, \strlen('macro_')), $object->getTemplateName()), $lineno, $source);
if (!method_exists($template, $method)) {
while ($parent = $template->getParent($context)) {
if (method_exists($parent, $method)) {
return $parent->$method(...$args);
}
}
throw new RuntimeError(sprintf('Macro "%s" is not defined in template "%s".', substr($method, \strlen('macro_')), $template->getTemplateName()), $lineno, $source);
}
return $object->$method(...$args);
return $template->$method(...$args);
}
/**
+1 -1
View File
@@ -57,7 +57,7 @@ class MethodCallExpression extends AbstractExpression
$compiler
->raw('], ')
->repr($this->getTemplateLine())
->raw(', $this->getSourceContext())');
->raw(', $context, $this->getSourceContext())');
}
}
@@ -0,0 +1,20 @@
--TEST--
"macro" tag
--TEMPLATE--
{% extends "parent" %}
{% block test %}
{{ _self.hello() }}
{% endblock test %}
--TEMPLATE(parent)--
{% block test %}
Hello
{% endblock test %}
{% macro hello() %}
Test
{% endmacro %}
--DATA--
return []
--EXPECT--
Test