Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T17:04:50.327Z
Answers.Text: 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!
Answers.UserDisplayName: Romain P.
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T17:19:23.103Z
Answers.Text: @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.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T16:38:48.213Z
Answers.LastActivityDate: 2014-08-15T16:38:48.213Z
Answers.UserDisplayName: carlcs
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T16:56:57.263Z
Answers.Text: I actually think the @carlcs solution is cleaner
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T16:57:10.763Z
Answers.Text: 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.
Answers.UserDisplayName: Romain P.
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T17:22:08.973Z
Answers.Text: Yes marion, but this solution actually fits the question title better AND you could do nice things for those letters that are empty.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T16:37:17.810Z
Answers.LastActivityDate: 2014-08-15T16:37:17.810Z
Answers.UserDisplayName: Marion Newlevant
Body: <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>
  
Tags: <forloop><entry><loop>
Title: Using a loop in a loop, how do I check if child loop is empty to not display parent loop?
ThreadText: # 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 %}
  
          <div>
              <h3>{{ letter | upper }}</h3>
              <ul> 
              {% for entry in letterEntries %}
                  {% if entry.intoCollection %}
                      <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
                  {% endif %}
              {% endfor %}
              </ul>
          </div>
  
          {% 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**: @username_0 this is a simple answer to your actual problem, but username_2'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](http://buildwithcraft.com/docs/lightswitch-fields) 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 %}
      <div>
        <h3>{{ letter | upper }}</h3>
        <ul> 
          {% for entry in letterEntries %}
            {% if entry.intoCollection %}
              <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
            {% endif %}
          {% endfor %}
        </ul>
      </div>
    {% endif %}
  
  {% endfor %}
  
  ```
  
  You can break the loop at `{# break here #}` if you have the [mnbreakandcontinue](https://github.com/username_2/craft-mnbreakandcontinue) plugin. It's not necessary, just a potential optimization.
  
  > **Comments:**
  > **username_2**: I actually think the @username_1 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 username_2, but this solution actually fits the question title better AND you could do nice things for those letters that are empty.  
  
  ---
  
  