New: Learn Statamic on Laracasts!

We've retired the forum in favor of GitHub Discussions.

New conversations can be started on GitHub. Existing conversations will remain for a while longer.

Head over to GitHub →

Multiple Fallback Conditions for Collection Loops

Lloyd Montgomery September 1, 2020 by Lloyd Montgomery


Statamic is able to do multiple conditions on loops over collections, as listed here: However, that appears to be an "AND" relationship. Does Statamic offer an "OR" relationship between multiple conditions? Additionally, can the conditions accommodate fallback values or null-avoidance?

Detailed Example

I am building a simple Posts page where users can filter the posts by selecting one "Area of Life" or "Tags" taxonomy entry. Once selected, the url is changed as such: href="{{ current_uri }}?tag={{ slug }}" such that the URI is now /posts?tag=5-min-research.

I have this selector working, and the associated generated list, but it is terribly verbose and has repeating code I would like to simplify.

{{ if get:aol }}

{{ collection:posts taxonomy:area_of_life="{ get:aol | sanitize }" }}
  {{ title }}
{{ /collection:posts }}

{{ elseif get:tag }}

{{ collection:posts taxonomy:post_tags="{ get:tag | sanitize }" }}
  {{ title }}
{{ /collection:posts }}

{{ else }}

{{ collection:posts }}
  {{ title }}
{{ /collection:posts }}

{{ /if }}

Keep in mind that I have simplified the above code. In the real version, {{ title }} is actually 15 lines of HTML Antlers with TailwindCSS. Those 15 lines are duplicated three times because I don't know a better way to get the collections loop functioning otherwise.

It really does feel like Statamic does support this in a different way, but I can't piece it together.


Answered by Erin Dalzell!
>>>>>>> Answered <<<<<<<
1 Reply