Partial.php 4.7 KB

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