<p>You'd further filter your returned artist entries down like this:</p>

<pre><code>{% set artists = craft.entries.section('artists').intoCollection('1') %}
</code></pre>

<p>Then remove all the unnecessary "intoCollection" conditionals in your loop.</p>

CC BY-SA 3.0
2014-08-15T17:04:50.327Z
I didn't know I can do like this. I'm a beginner so I thought it was more complicated than it is ;-) Thanks for your answer, I'll take note of this correct way to filter!
Romain P.
CC BY-SA 3.0
2014-08-15T17:19:23.103Z
@romain this is a simple answer to your actual problem, but marion's answer is actually the better answer to your problem description, that "check if child of loop in loop" kind of thing.
carlcs
CC BY-SA 3.0
2014-08-15T16:38:48.213Z
2014-08-15T16:38:48.213Z
carlcs
<p>You have <code>{% for entry in letterEntries %}</code> twice, one loop inside the other, which is going to give you things twice. Also, since <code>intoCollection</code> is a <a href="http://buildwithcraft.com/docs/lightswitch-fields" rel="nofollow">Lightswitch Field</a> you test it with <code>{% if entry.intoCollection %}</code> (no <code>|length</code>) Try this:</p>

<pre><code>{% set artists = craft.entries.section('artists').limit(null).order('title') %}
{% for letter, letterEntries in artists | group('title|upper|slice(0, 1)') %}

  {# see if any of the entries is intoCollection #}
  {% set isIntoCollection = false %}
  {% for entry in letterEntries %}
    {% if entry.intoCollection %}
      {% set isIntoCollection = true %}
      {# break here #}
    {% endif %}
  {% endfor %}

  {# now display them #}
  {% if isIntoCollection %}
    &lt;div&gt;
      &lt;h3&gt;{{ letter | upper }}&lt;/h3&gt;
      &lt;ul&gt; 
        {% for entry in letterEntries %}
          {% if entry.intoCollection %}
            &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
          {% endif %}
        {% endfor %}
      &lt;/ul&gt;
    &lt;/div&gt;
  {% endif %}

{% endfor %}
</code></pre>

<p>You can break the loop at <code>{# break here #}</code> if you have the <a href="https://github.com/marionnewlevant/craft-mnbreakandcontinue" rel="nofollow">mnbreakandcontinue</a> plugin. It's not necessary, just a potential optimization.</p>

CC BY-SA 3.0
2014-08-15T16:56:57.263Z
I actually think the @carlcs solution is cleaner
Marion Newlevant
CC BY-SA 3.0
2014-08-15T16:57:10.763Z
Thanks! It works, but it does not preserve my loop.index which I use for something else (close my divs rows): it seems to count everything, if entry.intoCollection or not. After one test with few entries, it jumps from 5 to 7.
Romain P.
CC BY-SA 3.0
2014-08-15T17:22:08.973Z
Yes marion, but this solution actually fits the question title better AND you could do nice things for those letters that are empty.
carlcs
CC BY-SA 3.0
2014-08-15T16:37:17.810Z
2014-08-15T16:37:17.810Z
Marion Newlevant
<p>I'm trying to display a list of names alphabetically ordered. This is working fine, but I also want to check if the name is <strong>intoCollection</strong> or not (using a <em>Lightswitch</em>).</p>

<p>If it is <strong>intoCollection</strong>, it displays the <em>letter</em> + the list of names related. If there is no name related to the <em>letter</em>, I need to display nothing (= no <em>letter</em>).</p>

<p>Here is the code which seems good for me, but is not correct:</p>

<pre><code>{% set artists = craft.entries.section('artists').limit(null).order('title') %}
{% for letter, letterEntries in artists | group('title|upper|slice(0, 1)') %}

    {% for entry in letterEntries %}
        {% if entry.intoCollection | length %}

        &lt;div&gt;
            &lt;h3&gt;{{ letter | upper }}&lt;/h3&gt;
            &lt;ul&gt; 
            {% for entry in letterEntries %}
                {% if entry.intoCollection %}
                    &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
                {% endif %}
            {% endfor %}
            &lt;/ul&gt;
        &lt;/div&gt;

        {% endif %}
    {% endfor %}

{% endfor %}
</code></pre>

<p>It displays <em>letters</em> duplicated, but also <em>letters</em> that contain no names...</p>

<p>Any idea? Thanks!</p>

<forloop><entry><loop>
Using a loop in a loop, how do I check if child loop is empty to not display parent loop?
# Using a loop in a loop, how do I check if child loop is empty to not display parent loop?

(Asked by: username_0 on 2014-08-15)

I'm trying to display a list of names alphabetically ordered. This is working fine, but I also want to check if the name is intoCollection or not (using a Lightswitch).
If it is intoCollection, it displays the letter + the list of names related. If there is no name related to the letter, I need to display nothing (= no letter).
Here is the code which seems good for me, but is not correct:
{% set artists = craft.entries.section('artists').limit(null).order('title') %}
{% for letter, letterEntries in artists | group('title|upper|slice(0, 1)') %}

    {% for entry in letterEntries %}
        {% if entry.intoCollection | length %}

        
            {{ letter | upper }}
             
            {% for entry in letterEntries %}
                {% if entry.intoCollection %}
                    {{ entry.title }}
                {% endif %}
            {% endfor %}
            
        

        {% endif %}
    {% endfor %}

{% endfor %}

It displays letters duplicated, but also letters that contain no names...
Any idea? Thanks!

**Tags:** forloop, entry, loop

---

## 2 answers

### Answer by username_1 on 2014-08-15. Score: 4 | Accepted answer

You'd further filter your returned artist entries down like this:
{% set artists = craft.entries.section('artists').intoCollection('1') %}

Then remove all the unnecessary "intoCollection" conditionals in your loop.

> **Comments:**
> **username_0**: I didn't know I can do like this. I'm a beginner so I thought it was more complicated than it is ;-) Thanks for your answer, I'll take note of this correct way to filter!  
> **username_1**: @romain this is a simple answer to your actual problem, but marion's answer is actually the better answer to your problem description, that "check if child of loop in loop" kind of thing.  

---

### Answer by username_2 on 2014-08-15. Score: 2

You have {% for entry in letterEntries %} twice, one loop inside the other, which is going to give you things twice. Also, since intoCollection is a Lightswitch Field you test it with {% if entry.intoCollection %} (no |length) Try this:
{% set artists = craft.entries.section('artists').limit(null).order('title') %}
{% for letter, letterEntries in artists | group('title|upper|slice(0, 1)') %}

  {# see if any of the entries is intoCollection #}
  {% set isIntoCollection = false %}
  {% for entry in letterEntries %}
    {% if entry.intoCollection %}
      {% set isIntoCollection = true %}
      {# break here #}
    {% endif %}
  {% endfor %}

  {# now display them #}
  {% if isIntoCollection %}
    
      {{ letter | upper }}
       
        {% for entry in letterEntries %}
          {% if entry.intoCollection %}
            {{ entry.title }}
          {% endif %}
        {% endfor %}
      
    
  {% endif %}

{% endfor %}

You can break the loop at {# break here #} if you have the mnbreakandcontinue plugin. It's not necessary, just a potential optimization.

> **Comments:**
> **username_2**: I actually think the @carlcs solution is cleaner  
> **username_0**: Thanks! It works, but it does not preserve my loop.index which I use for something else (close my divs rows): it seems to count everything, if entry.intoCollection or not. After one test with few entries, it jumps from 5 to 7.  
> **username_1**: Yes marion, but this solution actually fits the question title better AND you could do nice things for those letters that are empty.  

---

