3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\View\Helper
;
12 use Zend\View\Exception
;
13 use Zend\View\Model\ModelInterface
;
16 * Helper for rendering a template fragment in its own variable scope.
18 class Partial
extends AbstractHelper
21 * Variable to which object will be assigned
28 * Renders a template fragment within a variable scope distinct from the
29 * calling View object. It proxies to view's render function
31 * @param string|ModelInterface $name Name of view script, or a view model
32 * @param array|object $values Variables to populate in the view
33 * @throws Exception\RuntimeException
34 * @return string|Partial
36 public function __invoke($name = null, $values = null)
38 if (0 == func_num_args()) {
42 // If we were passed only a view model, just render it.
43 if ($name instanceof ModelInterface
) {
44 return $this->getView()->render($name);
47 if (is_scalar($values)) {
49 } elseif ($values instanceof ModelInterface
) {
50 $values = $values->getVariables();
51 } elseif (is_object($values)) {
52 if (null !== ($objectKey = $this->getObjectKey())) {
53 $values = array($objectKey => $values);
54 } elseif (method_exists($values, 'toArray')) {
55 $values = $values->toArray();
57 $values = get_object_vars($values);
61 return $this->getView()->render($name, $values);
70 public function setObjectKey($key)
73 $this->objectKey
= null;
77 $this->objectKey
= (string) $key;
85 * The objectKey is the variable to which an object in the iterator will be
90 public function getObjectKey()
92 return $this->objectKey
;