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

88 Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER

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.