32 /** |
32 /** |
33 * Helper for simplifying JSON responses |
33 * Helper for simplifying JSON responses |
34 * |
34 * |
35 * @package Zend_View |
35 * @package Zend_View |
36 * @subpackage Helper |
36 * @subpackage Helper |
37 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
37 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
38 * @license http://framework.zend.com/license/new-bsd New BSD License |
38 * @license http://framework.zend.com/license/new-bsd New BSD License |
39 */ |
39 */ |
40 class Zend_View_Helper_Json extends Zend_View_Helper_Abstract |
40 class Zend_View_Helper_Json extends Zend_View_Helper_Abstract |
41 { |
41 { |
42 /** |
42 /** |
43 * Encode data as JSON, disable layouts, and set response header |
43 * Encode data as JSON, disable layouts, and set response header |
44 * |
44 * |
45 * If $keepLayouts is true, does not disable layouts. |
45 * If $keepLayouts is true, does not disable layouts. |
|
46 * If $encodeJson is false, does not JSON-encode $data |
46 * |
47 * |
47 * @param mixed $data |
48 * @param mixed $data |
48 * @param bool $keepLayouts |
49 * @param bool $keepLayouts |
49 * NOTE: if boolean, establish $keepLayouts to true|false |
50 * NOTE: if boolean, establish $keepLayouts to true|false |
50 * if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false |
51 * if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false |
51 * this array can contains a 'keepLayout'=>true|false |
52 * this array can contains a 'keepLayout'=>true|false and/or 'encodeData'=>true|false |
52 * that will not be passed to Zend_Json::encode method but will be used here |
53 * that will not be passed to Zend_Json::encode method but will be used here |
|
54 * @param bool $encodeData |
53 * @return string|void |
55 * @return string|void |
54 */ |
56 */ |
55 public function json($data, $keepLayouts = false) |
57 public function json($data, $keepLayouts = false, $encodeData = true) |
56 { |
58 { |
57 $options = array(); |
59 $options = array(); |
58 if (is_array($keepLayouts)) |
60 if (is_array($keepLayouts)) { |
59 { |
61 $options = $keepLayouts; |
60 $options = $keepLayouts; |
62 |
61 $keepLayouts = (array_key_exists('keepLayouts', $keepLayouts)) |
63 $keepLayouts = false; |
62 ? $keepLayouts['keepLayouts'] |
64 if (array_key_exists('keepLayouts', $options)) { |
63 : false; |
65 $keepLayouts = $options['keepLayouts']; |
64 unset($options['keepLayouts']); |
66 unset($options['keepLayouts']); |
|
67 } |
|
68 |
|
69 if (array_key_exists('encodeData', $options)) { |
|
70 $encodeData = $options['encodeData']; |
|
71 unset($options['encodeData']); |
|
72 } |
65 } |
73 } |
66 |
74 |
67 $data = Zend_Json::encode($data, null, $options); |
75 if ($encodeData) { |
|
76 $data = Zend_Json::encode($data, null, $options); |
|
77 } |
68 if (!$keepLayouts) { |
78 if (!$keepLayouts) { |
69 require_once 'Zend/Layout.php'; |
79 require_once 'Zend/Layout.php'; |
70 $layout = Zend_Layout::getMvcInstance(); |
80 $layout = Zend_Layout::getMvcInstance(); |
71 if ($layout instanceof Zend_Layout) { |
81 if ($layout instanceof Zend_Layout) { |
72 $layout->disableLayout(); |
82 $layout->disableLayout(); |