Fixed Twig being very slow when templates do not exist

This commit is contained in:
Matias Griese 2021-12-09 21:09:23 +02:00
parent 6ed453890d
commit 491252476d
2 changed files with 38 additions and 0 deletions

View File

@ -15,6 +15,7 @@
* Fixed RequestHandlers `NotFoundException` having empty request
* Block `.json` files in web server configs
* Disabled pretty debug info for Flex as it slows down Twig rendering
* Fixed Twig being very slow when templates do not exist
# v1.7.25
## 11/16/2021

View File

@ -10,6 +10,9 @@
namespace Grav\Common\Twig;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Template;
use Twig\TemplateWrapper;
/**
* Class TwigEnvironment
@ -18,4 +21,38 @@ use Twig\Environment;
class TwigEnvironment extends Environment
{
use WriteCacheFileTrait;
/**
* @inheritDoc
*/
public function resolveTemplate($names)
{
if (!\is_array($names)) {
$names = [$names];
}
$count = \count($names);
foreach ($names as $name) {
if ($name instanceof Template) {
return $name;
}
if ($name instanceof TemplateWrapper) {
return $name;
}
if (1 !== $count && !$this->getLoader()->exists($name)) {
continue;
}
try {
return $this->loadTemplate($name);
} catch (LoaderError $e) {
if (1 === $count) {
throw $e;
}
}
}
throw new LoaderError(sprintf('Unable to find one of the following templates: "%s".', implode('", "', $names)));
}
}