mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-12 18:36:53 +00:00
added a better error message for unknown macros
This commit is contained in:
@@ -1311,6 +1311,18 @@ function twig_capitalize_string_filter(Environment $env, $string)
|
||||
return mb_strtoupper(mb_substr($string, 0, 1, $charset), $charset).mb_strtolower(mb_substr($string, 1, null, $charset), $charset);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
function twig_call_macro($object, string $method, array $args, int $lineno, 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);
|
||||
}
|
||||
|
||||
return $object->$method(...$args);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
|
||||
@@ -39,11 +39,11 @@ class MethodCallExpression extends AbstractExpression
|
||||
}
|
||||
|
||||
$compiler
|
||||
->raw('$macros[')
|
||||
->raw('twig_call_macro($macros[')
|
||||
->repr($this->getNode('node')->getAttribute('name'))
|
||||
->raw(']->')
|
||||
->raw($this->getAttribute('method'))
|
||||
->raw('(')
|
||||
->raw('], ')
|
||||
->repr($this->getAttribute('method'))
|
||||
->raw(', [')
|
||||
;
|
||||
$first = true;
|
||||
foreach ($this->getNode('arguments')->getKeyValuePairs() as $pair) {
|
||||
@@ -54,7 +54,10 @@ class MethodCallExpression extends AbstractExpression
|
||||
|
||||
$compiler->subcompile($pair['value']);
|
||||
}
|
||||
$compiler->raw(')');
|
||||
$compiler
|
||||
->raw('], ')
|
||||
->repr($this->getTemplateLine())
|
||||
->raw(', $this->getSourceContext())');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
--TEST--
|
||||
macro
|
||||
--TEMPLATE--
|
||||
{% import _self as macros %}
|
||||
|
||||
{{ macros.unknown() }}
|
||||
--DATA--
|
||||
return []
|
||||
--EXCEPTION--
|
||||
Twig\Error\RuntimeError: Macro "unknown" is not defined in template "index.twig" in "index.twig" at line 4.
|
||||
Reference in New Issue
Block a user