|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_View |
|
17 * @subpackage Helper |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @version $Id: Partial.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
20 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
21 */ |
|
22 |
|
23 /** Zend_View_Helper_Abstract.php */ |
|
24 require_once 'Zend/View/Helper/Abstract.php'; |
|
25 |
|
26 /** |
|
27 * Helper for rendering a template fragment in its own variable scope. |
|
28 * |
|
29 * @package Zend_View |
|
30 * @subpackage Helper |
|
31 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
32 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
33 */ |
|
34 class Zend_View_Helper_Partial extends Zend_View_Helper_Abstract |
|
35 { |
|
36 /** |
|
37 * Variable to which object will be assigned |
|
38 * @var string |
|
39 */ |
|
40 protected $_objectKey; |
|
41 |
|
42 /** |
|
43 * Renders a template fragment within a variable scope distinct from the |
|
44 * calling View object. |
|
45 * |
|
46 * If no arguments are passed, returns the helper instance. |
|
47 * |
|
48 * If the $model is an array, it is passed to the view object's assign() |
|
49 * method. |
|
50 * |
|
51 * If the $model is an object, it first checks to see if the object |
|
52 * implements a 'toArray' method; if so, it passes the result of that |
|
53 * method to to the view object's assign() method. Otherwise, the result of |
|
54 * get_object_vars() is passed. |
|
55 * |
|
56 * @param string $name Name of view script |
|
57 * @param string|array $module If $model is empty, and $module is an array, |
|
58 * these are the variables to populate in the |
|
59 * view. Otherwise, the module in which the |
|
60 * partial resides |
|
61 * @param array $model Variables to populate in the view |
|
62 * @return string|Zend_View_Helper_Partial |
|
63 */ |
|
64 public function partial($name = null, $module = null, $model = null) |
|
65 { |
|
66 if (0 == func_num_args()) { |
|
67 return $this; |
|
68 } |
|
69 |
|
70 $view = $this->cloneView(); |
|
71 if (isset($this->partialCounter)) { |
|
72 $view->partialCounter = $this->partialCounter; |
|
73 } |
|
74 if ((null !== $module) && is_string($module)) { |
|
75 require_once 'Zend/Controller/Front.php'; |
|
76 $moduleDir = Zend_Controller_Front::getInstance()->getControllerDirectory($module); |
|
77 if (null === $moduleDir) { |
|
78 require_once 'Zend/View/Helper/Partial/Exception.php'; |
|
79 $e = new Zend_View_Helper_Partial_Exception('Cannot render partial; module does not exist'); |
|
80 $e->setView($this->view); |
|
81 throw $e; |
|
82 } |
|
83 $viewsDir = dirname($moduleDir) . '/views'; |
|
84 $view->addBasePath($viewsDir); |
|
85 } elseif ((null == $model) && (null !== $module) |
|
86 && (is_array($module) || is_object($module))) |
|
87 { |
|
88 $model = $module; |
|
89 } |
|
90 |
|
91 if (!empty($model)) { |
|
92 if (is_array($model)) { |
|
93 $view->assign($model); |
|
94 } elseif (is_object($model)) { |
|
95 if (null !== ($objectKey = $this->getObjectKey())) { |
|
96 $view->assign($objectKey, $model); |
|
97 } elseif (method_exists($model, 'toArray')) { |
|
98 $view->assign($model->toArray()); |
|
99 } else { |
|
100 $view->assign(get_object_vars($model)); |
|
101 } |
|
102 } |
|
103 } |
|
104 |
|
105 return $view->render($name); |
|
106 } |
|
107 |
|
108 /** |
|
109 * Clone the current View |
|
110 * |
|
111 * @return Zend_View_Interface |
|
112 */ |
|
113 public function cloneView() |
|
114 { |
|
115 $view = clone $this->view; |
|
116 $view->clearVars(); |
|
117 return $view; |
|
118 } |
|
119 |
|
120 /** |
|
121 * Set object key |
|
122 * |
|
123 * @param string $key |
|
124 * @return Zend_View_Helper_Partial |
|
125 */ |
|
126 public function setObjectKey($key) |
|
127 { |
|
128 if (null === $key) { |
|
129 $this->_objectKey = null; |
|
130 } else { |
|
131 $this->_objectKey = (string) $key; |
|
132 } |
|
133 |
|
134 return $this; |
|
135 } |
|
136 |
|
137 /** |
|
138 * Retrieve object key |
|
139 * |
|
140 * The objectKey is the variable to which an object in the iterator will be |
|
141 * assigned. |
|
142 * |
|
143 * @return null|string |
|
144 */ |
|
145 public function getObjectKey() |
|
146 { |
|
147 return $this->_objectKey; |
|
148 } |
|
149 } |