views:

460

answers:

2

Hello everyone,

I want to to make a particular processing for the last item of a partialLoop, the documentation mention about $this->partialCounter but not the variable with the total number of items ...

<?php
if( $this->partialCounter == $mysteryvariable -1 ): 
?>

I am missing something I think ... cannot get my hand on that variable ...

+3  A: 

In order to get the total number of items, you will have to either extend Zend_View_Helper_PartialLoop to provide a method that returns the count of the iterable object being used by the PartialLoop.

Or, and I would say this is probably easier, just get the count of items in the object before you pass it into the PartialLoop since you have to pass either a Traversable object or an actual array into the PartialLoop helper and both implement support for count().

From the documentation:

<?php // partialLoop.phtml ?>
    <dt><?php echo $this->key ?></dt>
    <dd><?php echo $this->value ?></dd>


<?php // MyController.php

    public function indexAction()
    {
        $this->view->$model = array(
                                 array('key' => 'Mammal', 'value' => 'Camel'),
                                 array('key' => 'Bird', 'value' => 'Penguin'),
                                 array('key' => 'Reptile', 'value' => 'Asp'),
                                 array('key' => 'Fish', 'value' => 'Flounder'),
                              );

        $this->view->modelCount = count($this->view->model); 
    }

From index.phmtl

<p>Count: <?= $this->modelCount ?></p>
<dl>
<?php echo $this->partialLoop('partialLoop.phtml', $this->model) ?>
</dl>
Noah Goodrich
@gabriel1836: Thanks for the answer, what do you mean by pass it into the partialLoop? if you can write a small example
RageZ
I need the count in the partial ^^ like the last item is different from other items (i.e. no ending span ... )
RageZ
@RagZ - Are you saying that you want to know the current position of the array or iterable object and not just the total count of items in the array or object?
Noah Goodrich
+2  A: 
Zend_Registy::set('PartialCount', count($iterable));
$this->partialLoop($script,$iterable);

and then in your view

$count = Zend_Registy::get('PartialCount');

Fast and crappy, but working. Other way would be to to extend PartialLoop helper ;)

Tomáš Fejfar
yeah I suspected I can use the Registry but that's really hacky, lucky in this case I count make it work by changing the position of the offending tag so could make a test like `if($this->partialCounter == 1)` but that not really satifying .. wondering why they don't put a partialTotal or something like that
RageZ
other possibility is to inject the count to the iterabler object. Not array(0 => array('red' => 10), 1 =>array('red' => 5)); use array(0 => array('red' => 10, 'count' => 2), 1 =>array('red' => 5, 'count' => 2)); But that's also a hack and even more it's and less effective hack :D
Tomáš Fejfar