Forum

Thread tagged as: Question, Field-Types

Counting number of repeated items

Hi. I'm using a repeater and wanted to use <perch:if> to display a different layout if there are 1, 2 or 3 repeated sections. I'm sure this must be possible, but I'm not sure how.

Any ideas? Thank you!

David Maxwell-Wood

David Maxwell-Wood 0 points

  • 7 years ago

I think I've answered my own question.

I used

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

in each repeated section and then used

<perch:if id="perch_item_count" value="2">

to show the specific layout when the repeater contains 2 items. Hope this is the best way!

Hi David,

<perch:if id="id-of-repeater" value="3" match="gt">This will show if there are more than 3 repeated sections</perch:if>

Works for me

Thanks Dexter. I actually think I'm trying to do something that may not be possible...

It is possible to call particular fields from each repeated region?

For instance, I'd like to pull image 1 from repeated region 1 and image 2 from repeated region 2...?

I'm trying to build an HTML Email builder... so clients can quickly create HTMLs with their articles. Which means I want to do something like 'If section has 2 articles then use this template'. The problem is the HTML emails layout isn't uniform – there are spacers here and there and everywhere!

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can get the item count with:

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

Thanks Drew. Is there anyway to get at content from a particular item within a repeater? For instance if I wanted to do something like this:

    <perch:repeater id="email-section-1" label="Email Section 1" max="3">
        <perch:before>
            <div>       
        </perch:before>
                <div class="item1">
                    <!-- OUTPUT IMAGE FROM REPEATER ITEM1 -->
                </div>

                <div class="item2">
                    <div class="title">
                        <!-- OUTPUT TITLE FROM REPEATER ITEM2 -->
                    </div>
                </div>

                <div class="item2">
                    <div class="sub-title">
                        <!-- OUTPUT SUBTITLE FROM REPEATER ITEM3 -->
                    </div>
                </div>
        <perch:after>
            </div>
        </perch:after>
    </perch:repeater>
Drew McLellan

Drew McLellan 2638 points
Perch Support

You should be able to do:

<perch:if id="perch_item_index" match="eq" value="1">
    <div class="item1">
        <!-- OUTPUT IMAGE FROM REPEATER ITEM1 -->
    </div>
</perch:if>
<perch:if id="perch_item_index" match="eq" value="2">
    <div class="item2">
        <div class="title">
            <!-- OUTPUT TITLE FROM REPEATER ITEM2 -->
        </div>
    </div>
</perch:if>

and so on. The repeater is looped, so you need to think of it in that way.

Awesomeeeeee!

Thank you so much Drew!

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

Is it possible to use this in a <perch:after> block of the repeater with <perch:if> to output different content if there is 1 or more than one item in the repeater ?