Skip to content

Commit

Permalink
ixed exception caused by missing template type based on Accept: hea…
Browse files Browse the repository at this point in the history
…der #2705
  • Loading branch information
rhukster committed Oct 28, 2019
1 parent a93ef3f commit 99c6a78
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Updated built-in `composer.phar` to latest `1.9.0`
* Updated vendor libraries
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
Expand Down
40 changes: 22 additions & 18 deletions system/src/Grav/Common/Twig/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,17 +290,15 @@ public function processPage(PageInterface $item, $content = null)
$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);
}

Expand All @@ -310,8 +308,9 @@ public function processPage(PageInterface $item, $content = null)
$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;
Expand Down Expand Up @@ -394,7 +393,6 @@ public function processSite($format = null, array $vars = [])
$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);
Expand All @@ -403,23 +401,13 @@ public function processSite($format = null, array $vars = [])
}

// 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;
Expand Down Expand Up @@ -460,6 +448,22 @@ public function template($template)
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
*
Expand Down

0 comments on commit 99c6a78

Please sign in to comment.