diff --git a/CHANGELOG.md b/CHANGELOG.md index c9ede773c..902247ce7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ * Updated vendor libraries * Use `Symfony EventDispatcher` directly and not rockettheme/toolbox wrapper 1. [](#bugfix) + * Fixed exception caused by missing template type based on `Accept:` header [#2705](https://github.com/getgrav/grav/issues/2705) * Fixed `Page::untranslatedLanguages()` not being symmetrical to `Page::translatedLanguages()` * Fixed `Flex Pages` not calling `onPageProcessed` event when cached * Fixed phpstan issues in Framework up to level 7 diff --git a/composer.json b/composer.json index e84f36f67..832cf8703 100644 --- a/composer.json +++ b/composer.json @@ -45,7 +45,7 @@ "donatj/phpuseragentparser": "~0.10", "pimple/pimple": "~3.2", "rockettheme/toolbox": "~1.4", - "maximebf/debugbar": "~1.15", + "maximebf/debugbar": "~1.0", "league/climate": "^3.4", "antoligy/dom-string-iterators": "^1.0", "miljar/php-exif": "^0.6.4", diff --git a/system/src/Grav/Common/Twig/Twig.php b/system/src/Grav/Common/Twig/Twig.php index 63e769061..2984d6652 100644 --- a/system/src/Grav/Common/Twig/Twig.php +++ b/system/src/Grav/Common/Twig/Twig.php @@ -290,17 +290,15 @@ class Twig $twig_vars['page'] = $item; $twig_vars['media'] = $item->media(); $twig_vars['header'] = $item->header(); - $local_twig = clone $this->twig; $output = ''; + try { // Process Modular Twig if ($item->modularTwig()) { $twig_vars['content'] = $content; - $extension = $item->templateFormat(); - $extension = $extension ? ".{$extension}.twig" : TEMPLATE_EXT; - $template = $item->template() . $extension; + $template = $this->getPageTwigTemplate($item); $output = $content = $local_twig->render($template, $twig_vars); } @@ -310,8 +308,9 @@ class Twig $this->setTemplate($name, $content); $output = $local_twig->render($name, $twig_vars); } + } catch (LoaderError $e) { - throw new \RuntimeException($e->getRawMessage(), 404, $e); + throw new \RuntimeException($e->getRawMessage(), 400, $e); } return $output; @@ -394,7 +393,6 @@ class Twig $twig_vars['header'] = $page->header(); $twig_vars['media'] = $page->media(); $twig_vars['content'] = $content; - $ext = '.' . ($format ?: 'html') . TWIG_EXT; // determine if params are set, if so disable twig cache $params = $this->grav['uri']->params(null, true); @@ -403,23 +401,13 @@ class Twig } // Get Twig template layout - $template = $this->template($page->template() . $ext); + $template = $this->getPageTwigTemplate($page, $format); try { $output = $this->twig->render($template, $vars + $twig_vars); } catch (LoaderError $e) { $error_msg = $e->getMessage(); - // Try html version of this template if initial template was NOT html - if ($ext !== '.html' . TWIG_EXT) { - try { - $page->templateFormat('html'); - $output = $this->twig->render($page->template() . '.html' . TWIG_EXT, $vars + $twig_vars); - } catch (LoaderError $e) { - throw new \RuntimeException($error_msg, 400, $e); - } - } else { - throw new \RuntimeException($error_msg, 400, $e); - } + throw new \RuntimeException($error_msg, 400, $e); } return $output; @@ -460,6 +448,22 @@ class Twig return $this->template ?? $template; } + public function getPageTwigTemplate($page, $format = null) + { + $template = $page->template(); + $extension = $format ?: $page->templateFormat(); + $twig_extension = $extension ? '.'. $extension .TWIG_EXT : TEMPLATE_EXT; + $template_file = $this->template($page->template() . $twig_extension); + + if ($this->twig->getLoader()->exists($template_file)) { + return $template_file; + } else { + // Default to HTML + $page->templateFormat('html'); + return $template . TEMPLATE_EXT; + } + } + /** * Overrides the autoescape setting *