mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-30 20:16:45 +00:00
Improve error message
It is not obvious that you cannot use the Twig function `constant` to get the special `::class` constants of classes like so: `{{ constant('Twig\\Extension\\CoreExtension::class') }}`, due to the underlying PHP function `\constant()`.
The previous error message in this case: 'Constant "Twig\Extension\CoreExtension::class" is undefined.' was not very helpful.
This commit is contained in:
committed by
Fabien Potencier
parent
4c621adb49
commit
3963851a41
@@ -1456,6 +1456,10 @@ final class CoreExtension extends AbstractExtension
|
||||
}
|
||||
|
||||
if (!\defined($constant)) {
|
||||
if ('::class' === strtolower(substr($constant, -7))) {
|
||||
throw new RuntimeError(sprintf('You cannot use the Twig function "constant()" to access "%s". You could provide an object and call constant("class", $object) or use the class name directly as a string.', $constant));
|
||||
}
|
||||
|
||||
throw new RuntimeError(sprintf('Constant "%s" is undefined.', $constant));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user