Forum

Thread tagged as: Question

Repeater Count

Hi

I am trying to run an if statement to check that 5 items are present within a repeating region to allow it to be shown on screen else show a single image.

What is the best way to check the number of items in a repeater. My sample code is below.

<perch:if id="backgroundImages" match="eq" value="5">
<perch:repeater id="backgroundImages" label="Multi Background Images" max="5" order="2" divider-before="Must be 5 images to show as rotator">
<perch:before><div class="background"></perch:before>
    <figure style="background-image:url(<perch:content type="image" id="heroImage" label="Image" bucket="hero" width="2000" height="500" crop="true" help="Images must be at least 2000px x 400px" />);"></figure>
<perch:after></div></perch:after>
</perch:repeater>
<perch:else/>
<perch:if exists="singleImage">
<div class="backgroundSingle">
    <figure style="background-image:url(<perch:content type="image" id="singleImage" label="Single Image" bucket="hero" width="2000" height="500" crop="true" help="Images must be at least 2000px x 400px" order="1" />);"></figure>
</div>
<perch:else />
<div class="backgroundSingle">
    <figure style="background-image:url(/img/header-no-image.jpg);"></figure>
</div>
</perch:if>
</perch:if>
Neil Irwin

Neil Irwin 1 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

That's an interesting question. So you basically want something like a perch:if statement you could use to check the number of items in a repeater?

Hi Drew

Yeah. Basically I have built a CSS based animated background which works based on timed intervals and requires an overall time to be set in the stylesheet so needs to have a set number of images for the timings to work correctly.

So i am trying to use perch:if to check if there are 5 items in the repeating region and if there aren't carryout the perch:else statement and only display one image.

Thanks

Neil

Drew McLellan

Drew McLellan 2638 points
Perch Support

Nothing like that exists right now. The best you can do is fetch the region with both skip-template and return-html and only echo the HTML if there's enough items after inspecting the result.

No worries. Thanks Drew