Added ability to override the twig template format (html, son, xml, etc) via page header #1067

This commit is contained in:
Andy Miller
2016-09-30 12:12:52 -06:00
parent c3e74c2e09
commit c0c77fff67
4 changed files with 40 additions and 15 deletions

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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

View File

@@ -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());
};
}
}