Forum

Thread tagged as: Question

Accessing repeater total items outside loop

Is there anyway to access the total items contained within a repeater tag outside of the repeater loop?

For example, with the following sample code:

<perch:repeater id="images" label="Images">
    <img src="<perch:content type="image" id="image" label="Image" />" 
      alt="<perch:content type="text" id="alt" label="Description" />" />
</perch:repeater>

I want to be able to access the total number of items added to the images tag.

Cole Henley

Cole Henley 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I think you might be able to do this:

<perch:repeater id="images" output="count" />

Sadly not. Returns empty

:(

Drew McLellan

Drew McLellan 2638 points
Perch Support

Or this?

<perch:repeater id="images" output="count"></perch:repeater>

Still no joy

Drew McLellan

Drew McLellan 2638 points
Perch Support

Probably no then.

Okay - will have to do some client-side detection.

Thanks

Hi Drew

Would it be possible to make a feature request for this? Would be extremely useful.

Thanks,

Cole

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, of course.

I know this is a bit late and the thread is closed but would this give you the number you want?

<perch:repeater id="images">
    <perch:after><perch:content id="perch_item_count" type="hidden" /></perch:after>
</perch:repeater>

Or if after is a bit hacky, show it on the last item instead?

<perch:repeater id="images">
    <perch:if exists="perch_item_last"><perch:content id="perch_item_count" type="hidden" /></perch:if>
</perch:repeater>
<perch:repeater id="images">
    <perch:if exists="perch_item_last"><perch:content id="perch_item_count" type="hidden" /></perch:if>
</perch:repeater>

Mark this is a great solution and delivers what I need!