Skip to content

Commit

Permalink
starting to refactor views.
Browse files Browse the repository at this point in the history
Adding comments.
Incomplete.
  • Loading branch information
markstory committed Nov 28, 2011
1 parent c64e41e commit 011fd51
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
35 changes: 23 additions & 12 deletions lib/Cake/View/JsonView.php
@@ -1,9 +1,5 @@
<?php
/**
* A custom view class that is used for JSON responses
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -12,17 +8,31 @@
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.View
* @since CakePHP(tm) v 2.1.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::uses('View', 'View');

/**
* JsonView
* A view class that is used for JSON responses.
*
* By setting the 'serialize' key in your controller, you can specify a view variable
* that should be serialized to JSON and used as the response for the request.
* This allows you to omit views + layouts, if your just need to emit a single view
* variable as the JSON response.
*
* In your controller, you could do the following:
*
* `$this->set(array('posts' => $posts, 'serialize' => 'posts'));`
*
* When the view is rendered, the `$posts` view variable will be serialized
* into JSON.
*
* If you don't use the `serialize` key, you will need a view + layout just like a
* normal view.
*
* @package Cake.View
* @since CakePHP(tm) v 2.1.0
*/
class JsonView extends View {

Expand Down Expand Up @@ -53,13 +63,14 @@ public function __construct($controller) {
* @return string The rendered view.
*/
public function render($view = null, $layout = null) {
if (isset($this->viewVars['serialize'])) {
$serialize = $this->viewVars['serialize'];
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
return $this->output = json_encode($data);
}
if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
$this->_render($viewFileName);
return $this->output = $this->_render($viewFileName);
}

$data = isset($this->viewVars['serialize']) ? $this->viewVars['serialize'] : null;

return $this->output = json_encode($data);
}

}
26 changes: 19 additions & 7 deletions lib/Cake/View/XmlView.php
@@ -1,9 +1,5 @@
<?php
/**
* A custom view class that is used for XML responses
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -12,18 +8,34 @@
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.View
* @since CakePHP(tm) v 2.1.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::uses('View', 'View');
App::uses('Xml', 'Utility');

/**
* XmlView
* A view class that is used for creating XML responses.
*
* By setting the 'serialize' key in your controller, you can specify a view variable
* that should be serialized to XML and used as the response for the request.
* This allows you to omit views + layouts, if your just need to emit a single view
* variable as the XML response.
*
* In your controller, you could do the following:
*
* `$this->set(array('posts' => $posts, 'serialize' => 'posts'));`
*
* When the view is rendered, the `$posts` view variable will be serialized
* into XML.
*
* **Note** The view variable you specify must be compatible with Xml::fromArray().
*
* If you don't use the `serialize` key, you will need a view + layout just like a
* normal view.
*
* @package Cake.View
* @since CakePHP(tm) v 2.1.0
*/
class XmlView extends View {

Expand Down

0 comments on commit 011fd51

Please sign in to comment.