Exemple #1
0
 /**
  * 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 direct($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'))) {
         $e = new 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;
     foreach ($model as $item) {
         // increment the counter variable
         $this->partialCounter++;
         $content .= parent::direct($name, $module, $item);
     }
     return $content;
 }
Exemple #2
0
    /**
     * 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\Partial
     */
    public function direct($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 ((null !== $module) && is_string($module)) {
            $moduleDir = \Zend\Controller\Front::getInstance()->getControllerDirectory($module);
            if (null === $moduleDir) {
                $e = new 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->vars()->assign($model);
            } elseif (is_object($model)) {
                if (null !== ($objectKey = $this->getObjectKey())) {
                    $view->vars()->assign($objectKey, $model);
                } elseif (method_exists($model, 'toArray')) {
                    $view->vars()->assign($model->toArray());
                } else {
                    $view->vars()->assign(get_object_vars($model));
                }
            }
        }

        return $view->render($name);
    }