Forum

Thread tagged as: Question

implementing an OR statement using

I'm just getting into perch so I may be missing something here but it seems that the conditional logic in perch doesn't lend itself to a DRY approach. For example, there doesn't appear to be a way to do the following (using PHP as example):

if (isset($item1) || isset($item2)) { display content };

The specific example is in the code snippet below. I'm trying to prevent the parent container (.content-block) from appearing unless either the 'shout' content or the 'body' content is populated. I've seen suggestions in the forums that I can use nested if/else statements but that will require repeating code at least once. Is there any way around this?

example:

<perch:if exists="shout" OR "body">
   <div class="content">
         <perch:if exists="shout">
              <h2 class="shout"><perch:content id="shout" type="text" label="Shout heading" /></h2>
         </perch:if>
        <perch:content id="body" type="textarea" label="Body" markdown="true" editor="markitup" />
    </div>
</perch:if>
Alex Ward

Alex Ward 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You've just quoted it incorrectly:

<perch:if exists="shout OR body">
    ...
</perch:if>

Ah that's interesting. Good to know it is something that exists then :) Oddly it seems to be resolving as true even if neither id has content though.

    <perch:if exists="shout OR body">
        <div class="content">
            <perch:if exists="shout"><h2 class="shout"><perch:content id="shout" type="text" label="Shout heading" /></h2>  </perch:if>
            <perch:content id="body" type="textarea" label="Body" markdown="true" editor="markitup" />
        </div>
    <perch:if>

Am I still doing it wrong in some way?

Drew McLellan

Drew McLellan 2638 points
Perch Support

What does <perch:showall /> output?

ID  Value
perch_page_path /perch/core/apps/content/edit/index.php
_id 17
heading Why choose BBQ?
_title  Why choose BBQ?
shout   
body    
has_cta 
button_url  
button_type dark
button_text 
itemRowID   131
itemID  17
regionID    14
pageID  4
itemRev 2
itemOrder   1000
itemJSON    {"_id":"17","heading":"Why choose BBQ?","_title":"Why choose{...}
itemSearch  Why choose BBQ? dark
itemUpdated 2014-11-06 11:58:11
itemUpdatedBy   1
perch_item_first    true
perch_item_last true
perch_item_zero_index   0
perch_item_index    1
perch_item_odd  
perch_item_count    1
Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you confirm you're running the latest version of Perch?

Perch: 2.7.2 downloaded from the site on Tuesday. I assume it's the latest version.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Should be good enough.

All my unit tests are passing for that logic, so it should be fine.

How are you displaying the region? If using perch_content() have you re-saved after changing the template?

Yeah I'm using perch_content() and I've re-saved. Not sure what's going on then. I'll have to keep digging. I'll let you know if I find anything :)