web/lib/Zend/View/Helper/PartialLoop.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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: PartialLoop.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_Partial */
       
    24 require_once 'Zend/View/Helper/Partial.php';
       
    25 
       
    26 /**
       
    27  * Helper for rendering a template fragment in its own variable scope; iterates
       
    28  * over data provided and renders for each iteration.
       
    29  *
       
    30  * @package    Zend_View
       
    31  * @subpackage Helper
       
    32  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    33  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    34  */
       
    35 class Zend_View_Helper_PartialLoop extends Zend_View_Helper_Partial
       
    36 {
       
    37 
       
    38     /**
       
    39      * Marker to where the pointer is at in the loop
       
    40      * @var integer
       
    41      */
       
    42     protected $partialCounter = 0;
       
    43 
       
    44     /**
       
    45      * Renders a template fragment within a variable scope distinct from the
       
    46      * calling View object.
       
    47      *
       
    48      * If no arguments are provided, returns object instance.
       
    49      *
       
    50      * @param  string $name Name of view script
       
    51      * @param  string|array $module If $model is empty, and $module is an array,
       
    52      *                              these are the variables to populate in the
       
    53      *                              view. Otherwise, the module in which the
       
    54      *                              partial resides
       
    55      * @param  array $model Variables to populate in the view
       
    56      * @return string
       
    57      */
       
    58     public function partialLoop($name = null, $module = null, $model = null)
       
    59     {
       
    60         if (0 == func_num_args()) {
       
    61             return $this;
       
    62         }
       
    63 
       
    64         if ((null === $model) && (null !== $module)) {
       
    65             $model  = $module;
       
    66             $module = null;
       
    67         }
       
    68 
       
    69         if (!is_array($model)
       
    70             && (!$model instanceof Traversable)
       
    71             && (is_object($model) && !method_exists($model, 'toArray'))
       
    72         ) {
       
    73             require_once 'Zend/View/Helper/Partial/Exception.php';
       
    74             $e = new Zend_View_Helper_Partial_Exception('PartialLoop helper requires iterable data');
       
    75             $e->setView($this->view);
       
    76             throw $e;
       
    77         }
       
    78 
       
    79         if (is_object($model)
       
    80             && (!$model instanceof Traversable)
       
    81             && method_exists($model, 'toArray')
       
    82         ) {
       
    83             $model = $model->toArray();
       
    84         }
       
    85 
       
    86         $content = '';
       
    87         // reset the counter if it's call again
       
    88         $this->partialCounter = 0;
       
    89         foreach ($model as $item) {
       
    90             // increment the counter variable
       
    91             $this->partialCounter++;
       
    92 
       
    93             $content .= $this->partial($name, $module, $item);
       
    94         }
       
    95 
       
    96         return $content;
       
    97     }
       
    98 }