diff --git a/CHANGELOG.md b/CHANGELOG.md index c3317ff64b..255edfc76e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # v1.7.0-rc.17 ## mm/dd/2020 +1. [](#improved) + * Fall back through various templates scenarios if they don't exist in theme to avoid unhelpful error. + * Added default templates for `external.html.twig`, `default.html.twig`, and `modular.html.twig` 1. [](#bugfix) * Fixed `Security::sanitizeSVG()` creating an empty file if SVG file cannot be parsed * Fixed infinite loop in blueprints with `extend@` to a parent stream diff --git a/system/src/Grav/Common/Twig/Twig.php b/system/src/Grav/Common/Twig/Twig.php index 8d16cacd8d..79326223f5 100644 --- a/system/src/Grav/Common/Twig/Twig.php +++ b/system/src/Grav/Common/Twig/Twig.php @@ -445,14 +445,31 @@ public function getPageTwigTemplate($page, $format = null) $twig_extension = $extension ? '.'. $extension .TWIG_EXT : TEMPLATE_EXT; $template_file = $this->template($page->template() . $twig_extension); + $page_template = null; + $loader = $this->twig->getLoader(); - if ($loader instanceof ExistsLoaderInterface && $loader->exists($template_file)) { - return $template_file; + if ($loader instanceof ExistsLoaderInterface ) { + + if ($loader->exists($template_file)) { + $page_template = $template_file; + } else { + // Try with template + html.twig + if ($twig_extension !== TEMPLATE_EXT && $loader->exists($template . TEMPLATE_EXT)) { + $page_template = $template . TEMPLATE_EXT; + // Try with default and original extension + } elseif ($loader->exists('default' . $twig_extension)) { + $page_template = 'default' . $twig_extension; + // Else try default + default extension + } elseif (!$page->modular() && $loader->exists('default' . TEMPLATE_EXT)) { + $page_template = 'default' . TEMPLATE_EXT; + } else { + $page_template = 'modular/default' . TEMPLATE_EXT; + } + } } - // Default to HTML - $page->templateFormat('html'); - return $template . TEMPLATE_EXT; + return $page_template; + } /** diff --git a/system/templates/default.html.twig b/system/templates/default.html.twig new file mode 100644 index 0000000000..f18206b771 --- /dev/null +++ b/system/templates/default.html.twig @@ -0,0 +1,4 @@ +{# Default output if no theme #} +
{{ page.template() ~'.'~ page.templateFormat() ~".twig" }}
template not found for page: {{ page.route() }}
{{ page.template() ~'.'~ page.templateFormat() ~".twig" }}
template not found for page: {{ page.route() }}