So I have a section called children and these are related to various other entries include users. I am fetching all the children for the currentUser like so and eager-loading all the other relationships.
{% set childrenQuery = craft.entries
.section('children')
.relatedTo({
sourceElement: currentUser,
field: 'relatedChildren'
})
.with([
'associatedTerm',
'tuitionClass',
'tuitionClass.location'
]) %}
{% set children = childrenQuery.all() %}
What I then want to do is split out children in to 'Active' and 'Inactive' depending on if the related field associatedTerm is live.
I can do this fine like this;
Children (active)<br />
<ul class="list-disc mb-6">
{% for child in children if child.associatedTerm|length %}
<li>{{ child.firstName }} {{ child.surname }}</li>
<li>{{ child.tuitionClass[0] }}</li>
<li>{{ child.associatedTerm[0] }}</li>
<li>{{ child.tuitionClass[0].location[0] }}</li>
{% endfor %}
</ul>
Children Inactive ()<br />
<ul class="list-disc mb-6">
{% for child in children if not child.associatedTerm|length %}
<li>{{ child.firstName }} {{ child.surname }}</li>
{% endfor %}
</ul>
However, what I want to do is actually do this logic first, so I can then wrap the active/inactive blocks into if statements, if there are in fact any active or inactive children as I have other code (headings and text) I want to show within the block and not appear at all if they are no entries.
So I am stuck on how to do this {% for child in children if not child.associatedTerm|length %} without a for loop and just check if there are any entries that exist.
I can achieve it like this; but it feels a little weird.
{% set childrenAreInactive = '' %}
{% for child in children if not child.associatedTerm|length %}
{% set childrenAreInactive = 1 %}
{% endfor %}
Anyone have any ideas for a better method?