Raymond Camden
Raymond Camden

Reputation: 10857

How can I conditionally show a section based on if a filtered array would show items?

I'm using Document Generation and have used filtering in lists like so:

{% repeating-section cats[weight > 5] %}

This will loop over a cats array and only show items where the weight property is over 5. This works fine, but I'm using it in a "section" with an intro and I want to hide the entire section if that particular array is empty. How can I use that in a condition?

Upvotes: 1

Views: 105

Answers (1)

Raymond Camden
Raymond Camden

Reputation: 10857

Using the JSONata docs, there is a $count operator. This can be used to count the number of items in an array, and include the filter as well. So for example:

{% conditional-section expr($count(cats[weight > 5]) > 0) %}

This can be used around the section that includes your list.

Upvotes: 1

Related Questions