Skip to content

Commit

Permalink
Added ability to override the twig template format (html, son, xml, e…
Browse files Browse the repository at this point in the history
…tc) via page header #1067
  • Loading branch information
rhukster committed Sep 30, 2016
1 parent c3e74c2 commit c0c77ff
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions system/src/Grav/Common/Grav.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,12 @@ public function mime($format)
*/
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();
Expand All @@ -279,7 +279,7 @@ public function header()
}

// Set debugger data in headers
if (!($extension === null || $extension == 'html')) {
if (!($format === null || $format == 'html')) {
$this['debugger']->enabled(false);
}

Expand Down
40 changes: 32 additions & 8 deletions system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -420,6 +421,9 @@ public function header($var = null)
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;
Expand Down Expand Up @@ -1100,24 +1104,44 @@ public function template($var = null)
}

/**
* Gets and sets the extension field.
* 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|string
* @return null
*/
public function extension($var = null)
public function templateFormat($var = null)
{
if ($var !== null) {
$this->extension = $var;
$this->template_format = $var;
}
if (empty($this->extension)) {
$this->extension = '.' . pathinfo($this->name(), PATHINFO_EXTENSION);

if (empty($this->template_format)) {
$this->template_format = Grav::instance()['uri']->extension();
}

return $this->extension;
return $this->template_format;
}

/**
* Gets and sets the extension field.
*
* @param null $var
*
* @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;
}

/**
* Returns the page extension, got from the page `url_extension` config and falls back to the
* system config `system.pages.append_url_extension`.
Expand Down
3 changes: 1 addition & 2 deletions system/src/Grav/Common/Service/OutputServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
}
}

0 comments on commit c0c77ff

Please sign in to comment.