TransWikia.com

Simplify segment conditional documentation

ExpressionEngine® Answers Asked by Stéphane on October 3, 2021

Exist an interesting method to simplify a multiple conditional:

If you want to execute the code when url_title not equal to “portfolio” neither equal to “photos” use this condition:

{if segment_2 != "portfolio" && segment_2 != "photos"}
    <p>Conditional content.</p>
{/if}

Or as a simple way:

{if ! "portfolio|photos" *= segment_2}
    I am here
{/if}

It works fine, does exist a documentation for this pattern:

segment_2 != "portfolio" && segment_2 != "photos" 

to

! "portfolio|photos" *= segment_2  ?

I didn’t find it.

One Answer

The conditional operators that work in EE are listed in the documentation.

The two conditional forms you talk about are not equivalent, they just happen to give the same outputs for the inputs you describe.

The first form (with the && in it) only fires if the url_title is not portfolio and also not photos. So if you tested the url_title hot with this conditional it would return TRUE (since hot is neither portfolio nor photos).

The *= term in second form tests to see if the the content of first term (in your example the string portfolio|photos contains the value in segment_2. When you add the negative operator (!) this returns TRUE only if the content of segment_2 is not contained within the string. This is similar in function to the first conditional but not the same: it will return "FALSE" for any circumstance where the content of segment_2 is found in your string - so not only for the url_titles portfolio and photos but also hot, folio and port (and many more).

So while the first conditional only looks for the two terms specified, the second looks for any combination of letters that might appear within the conjoined strings you have used. Which means the second conditional is not a simplification of the first, it is different to the first but under some conditions provides similar outputs.

HTH

Correct answer by JCOGS Design on October 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP