diff --git a/CHANGELOG.md b/CHANGELOG.md index 842c6ff94..a3a253abd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # v1.1.6 ## XX/XX/2016 +1. [](#new) + * Added ability for Page to override the output format (`html`, `xml`, etc..) [#1067](https://github.com/getgrav/grav/issues/1067) 1. [](#improved) * Add `batch()` function to Page Collection class * Added new `cache.redis.socket` setting that allow to pass a UNIX socket as redis server diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index 05041f9bd..7666dc637 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -251,12 +251,12 @@ class Grav extends Container */ public function header() { - $extension = $this['uri']->extension(); - /** @var Page $page */ $page = $this['page']; - header('Content-type: ' . $this->mime($extension)); + $format = $page->templateFormat(); + + header('Content-type: ' . $this->mime($format)); // Calculate Expires Headers if set to > 0 $expires = $page->expires(); @@ -279,7 +279,7 @@ class Grav extends Container } // Set debugger data in headers - if (!($extension === null || $extension == 'html')) { + if (!($format === null || $format == 'html')) { $this['debugger']->enabled(false); } diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index 0062d609a..3ff1399b8 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -85,6 +85,7 @@ class Page protected $home_route; protected $hide_home_route; protected $ssl; + protected $template_format; /** * @var Page Unmodified (original) version of the page. Used for copying and moving the page. @@ -420,6 +421,9 @@ class Page if (isset($this->header->ssl)) { $this->ssl = (bool) $this->header->ssl; } + if (isset($this->header->template_format)) { + $this->template_format = $this->header->template_format; + } } return $this->header; @@ -1099,6 +1103,26 @@ class Page return $this->template; } + /** + * Allows a page to override the output render format, usually the extension provided + * in the URL. (e.g. `html`, `json`, `xml`, etc). + * + * @param null $var + * @return null + */ + public function templateFormat($var = null) + { + if ($var !== null) { + $this->template_format = $var; + } + + if (empty($this->template_format)) { + $this->template_format = Grav::instance()['uri']->extension(); + } + + return $this->template_format; + } + /** * Gets and sets the extension field. * @@ -1107,16 +1131,16 @@ class Page * @return null|string */ public function extension($var = null) - { - if ($var !== null) { - $this->extension = $var; - } - if (empty($this->extension)) { - $this->extension = '.' . pathinfo($this->name(), PATHINFO_EXTENSION); - } - - return $this->extension; +{ + if ($var !== null) { + $this->extension = $var; } + if (empty($this->extension)) { + $this->extension = '.' . pathinfo($this->name(), PATHINFO_EXTENSION); + } + + return $this->extension; +} /** * Returns the page extension, got from the page `url_extension` config and falls back to the diff --git a/system/src/Grav/Common/Service/OutputServiceProvider.php b/system/src/Grav/Common/Service/OutputServiceProvider.php index 4c0805580..824f0f22c 100644 --- a/system/src/Grav/Common/Service/OutputServiceProvider.php +++ b/system/src/Grav/Common/Service/OutputServiceProvider.php @@ -15,8 +15,7 @@ class OutputServiceProvider implements ServiceProviderInterface { public function register(Container $container) { $container['output'] = function ($c) { - /** @var Grav $c */ - return $c['twig']->processSite($c['uri']->extension()); + return $c['twig']->processSite($c['page']->templateFormat()); }; } }