Permalink
Browse files

starting to refactor views.

Adding comments.
Incomplete.
  • Loading branch information...
markstory committed Nov 7, 2011
1 parent c64e41e commit 011fd5153945e401ab3d4cea1b45a61948b94958
Showing with 42 additions and 19 deletions.
  1. +23 −12 lib/Cake/View/JsonView.php
  2. +19 −7 lib/Cake/View/XmlView.php
View
@@ -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)
*
@@ -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 {
@@ -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);
}
}
View
@@ -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)
*
@@ -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 {

0 comments on commit 011fd51

Please sign in to comment.