0

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.

1 Answer 1

2

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

4
  • Hi JCOGS, thanks for your response. Really I already read many times the doc, and I never saw the *= term. It's a nice solution to simplify complex conditionals.
    – Stéphane
    Commented Jun 14, 2020 at 0:44
  • Why is the negative operator (NOT) not listed on this page of the documentation? eg {if !logged_in}...{/if} Commented Jun 14, 2020 at 5:18
  • The NOT operator is listed in the documentation under Logical Operators - here: docs.expressionengine.com/latest/templates/…. You would have seen it if you had thought to scroll down the page a bit. Commented Jun 14, 2020 at 14:03
  • True, thanks... but no example of what would be a pretty common shorthand use case. A better case might be {if !segment_3}...{/if} Commented Jun 14, 2020 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.