/** Element view helper. */ public function element($name = null, $module = null, $model = null) { $view = $this->view; if (null !== $module && is_string($module)) { // require_once 'Zend/Controller/Front.php'; $moduleDir = Zend_Controller_Front::getInstance()->getControllerDirectory($module); if (null === $moduleDir) { // require_once 'Zend/View/Helper/Partial/Exception.php'; $e = new Zend_View_Helper_Partial_Exception('Cannot render partial; module does not exist'); $e->setView($this->view); throw $e; } $viewsDir = dirname($moduleDir) . '/views'; $view->addBasePath($viewsDir); } elseif (null == $model && null !== $module && (is_array($module) || is_object($module))) { $model = $module; } if (!empty($model)) { if (is_array($model)) { $view->assign($model); } elseif (is_object($model)) { if (null !== ($objectKey = $this->getObjectKey())) { $view->assign($objectKey, $model); } elseif (method_exists($model, 'toArray')) { $view->assign($model->toArray()); } else { $view->assign(get_object_vars($model)); } } } return $view->render('element/' . $name . '.phtml'); }
/** * Renders a template fragment within a variable scope distinct from the * calling View object. * * If no arguments are provided, returns object instance. * * @param string $name Name of view script * @param string|array $module If $model is empty, and $module is an array, * these are the variables to populate in the * view. Otherwise, the module in which the * partial resides * @param array $model Variables to populate in the view * @return string */ public function partialLoop($name = null, $module = null, $model = null) { if (0 == func_num_args()) { return $this; } if (null === $model && null !== $module) { $model = $module; $module = null; } if (!is_array($model) && !$model instanceof Traversable && (is_object($model) && !method_exists($model, 'toArray'))) { #require_once 'Zend/View/Helper/Partial/Exception.php'; $e = new Zend_View_Helper_Partial_Exception('PartialLoop helper requires iterable data'); $e->setView($this->view); throw $e; } if (is_object($model) && !$model instanceof Traversable && method_exists($model, 'toArray')) { $model = $model->toArray(); } $content = ''; // reset the counter if it's call again $this->partialCounter = 0; $this->partialTotalCount = count($model); foreach ($model as $item) { // increment the counter variable $this->partialCounter++; $content .= $this->partial($name, $module, $item); } return $content; }
/** * Renders a template fragment within a variable scope distinct from the * calling View object. * * If no arguments are passed, returns the helper instance. * * If the $model is an array, it is passed to the view object's assign() * method. * * If the $model is an object, it first checks to see if the object * implements a 'toArray' method; if so, it passes the result of that * method to to the view object's assign() method. Otherwise, the result of * get_object_vars() is passed. * * @param string $name Name of view script * @param string|array $module If $model is empty, and $module is an array, * these are the variables to populate in the * view. Otherwise, the module in which the * partial resides * @param array $model Variables to populate in the view * @return string|Zend_View_Helper_Partial */ public function partial($name = null, $module = null, $model = null) { if (0 == func_num_args()) { return $this; } $view = $this->cloneView(); if (isset($this->partialCounter)) { $view->partialCounter = $this->partialCounter; } if (isset($this->partialTotalCount)) { $view->partialTotalCount = $this->partialTotalCount; } if (null !== $module && is_string($module)) { require_once LIB_DIR . '/Zend/Controller/Front.php'; $moduleDir = Zend_Controller_Front::getInstance()->getControllerDirectory($module); if (null === $moduleDir) { require_once LIB_DIR . '/Zend/View/Helper/Partial/Exception.php'; $e = new Zend_View_Helper_Partial_Exception('Cannot render partial; module does not exist'); $e->setView($this->view); throw $e; } $viewsDir = dirname($moduleDir) . '/views'; $view->addBasePath($viewsDir); } elseif (null == $model && null !== $module && (is_array($module) || is_object($module))) { $model = $module; } if (!empty($model)) { if (is_array($model)) { $view->assign($model); } elseif (is_object($model)) { if (null !== ($objectKey = $this->getObjectKey())) { $view->assign($objectKey, $model); } elseif (method_exists($model, 'toArray')) { $view->assign($model->toArray()); } else { $view->assign(get_object_vars($model)); } } } return $view->render($name); }