twigphp / Twig

Twig, the flexible, fast, and secure template language for PHP
https://twig.symfony.com/
BSD 3-Clause "New" or "Revised" License
8.17k stars 1.25k forks source link

Errors thrown for an invalid parent don't report their file and line as the Twig template #3266

Open stof opened 4 years ago

stof commented 4 years ago

See the fiddle at https://twigfiddle.com/hmgy4z

The template name and the line are reported as part of the message, not as part of the exception location, as done for most other Twig errors (and so the Symfony error page would not extract a code snippet from the Twig file).

The cause is that the location is set by using \Twig\Template::loadTemplate in the compile code (see the compilation logic) but that method takes only a template name, not a source context. And when it builds a source context from it, it cannot give it a source path, even if the template had the info.

A suggestion to fix that would be to change \Twig\Template::loadTemplate to accept a SourceContext rather than a template name (we might support both if needed) for cases which have it (most places now, as the compiled code always extract the name from the source context anyway).

fabpot commented 4 years ago

@stof Would you like to submit a PR?

stof commented 4 years ago

@fabpot I'll do it.

fabpot commented 1 month ago

@stof By any chance, can you have a look at this one?