mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-31 20:47:28 +00:00
fixed macro inheritance
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user