Forum

Thread tagged as: Question

Perch if - test for either of the two exists

Hi,

I want to use Perch if to see if either of these exist, if one or both do then show the content. Using OR seems to only show them if both match which doesn't make sense?

<perch:if exists="case-study-quote OR awards">
...

</perch:if>
Sarah Evans

Sarah Evans 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

That looks like it should work - what does <perch:showall /> output?

case-study-quote always gets outputted with a value or without one awards only seems to if there is one.

awards is a repeater, would this make a difference? The full code:


<perch:if exists="case-study-quote OR awards"> <div class="col-8 col-med-24 last bg-cream case-study-content--col case-study-content--side"> <div class="inner"> <perch:if exists="case-study-quote"> <perch:if exists="awards"> <div class="col-med-12 "> <perch:else /> <div class="col-med-24 "> </perch:if> <h3>Client Quote</h3> <div class="case-study-side client-quote"> <blockquote><p> <perch:content id="case-study-quote" type="textarea" label="Case Study Quote" size="s" divider-before="Client Quote" /></p> </blockquote> <p><perch:content id="case-study-quote-name" type="text" label="Case Study Name" /></p> </div> </div> </perch:if> <perch:if exists="awards"> <div class="col-med-12 med-last case-study-side awards"> <h3>Awards</h3> <perch:repeater id="awards" label="Awards" divider-before="Awards"> <perch:before> <ul> </perch:before> <li><perch:content id="award" type="text" label="Award" size="xl" /></li> <perch:after> </ul> </perch:after> </perch:repeater> </div> </perch:if> </div> </div> </perch:if>
Drew McLellan

Drew McLellan 2638 points
Perch Support

You can't test for the existence of a repeater - you have to check for the existence of content.

Do you see the repeater values output in perch:showall ?

I get this if there is an award


awards Array ( [0] => Array ( [award] => The content ) )

I have been testing against the repeater (awards) to display its sub section. If I just test against the content (award) it doesn't work

Drew McLellan

Drew McLellan 2638 points
Perch Support

Do you don't get an awards.award item?

Nope, all I get is the above.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, that's odd. I'm not sure how to help in that case. Is it only the OR that fails?

Just done some testing using other content.

It always works as expected on individual items when I add OR using two content ids that previously worked on their own they no longer do.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Only when they're repeaters?

What I've just been testing aren't content with repeaters.

It works individually, not together with OR

Below example "case-study-quote" does exist "main-heading" does exist "video" doesn't exist

Hello appears:


<perch:if exists="case-study-quote"> Hello </perch:if>

Hello appears


<perch:if exists="main-heading"> Hello </perch:if>

Hello doesn't appear


<perch:if exists="case-study-quote OR main-heading"> Hello </perch:if>

Hello doesn't appear


<perch:if exists="video OR main-heading"> Hello </perch:if>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Dashes aren't actually legal in IDs. I wonder if that's a contributing factor.

All my unit tests for this functionality are passing, so there's obviously some subtlety to it.

That's done the trick. I now need to get out of the habit of using dashes. Thanks Drew!