<p>You can use the <a href="https://docs.craftcms.com/v3/templating/filters.html#group" rel="nofollow noreferrer">group</a> filter</p>

<pre><code>{% set allEntries = craft.entries.section('pets').all() %}
{% set allEntriesByType = allEntries|group('type') %}
{% for type, pets in allEntriesByType %}

     All Pets of Type {{ type }}

     {% for pet in pets %}
         {% include '_partials/animalOrgChart' with {
             title: pet.title,
             subTitle: pet.jobTitle,
             img: pet.headshot|length ? pet.headshot[0],                      
             url: pet.url,
             portrait: true
         } %}        
    {% endfor %}

    &lt;hr class="line" /&gt; 
{% endfor %}
</code></pre>

CC BY-SA 4.0
2018-05-18T13:20:11.373Z
This is great, so simple and concise. For {% set allEntriesByType = allEntries|group('type') %}, I'm targeting a field handle. It works great @Robin. Thanks again.
TRIM
CC BY-SA 4.0
2018-05-17T13:46:18.090Z
2018-05-17T13:46:18.090Z
Robin Schambach
<p>I'm looking for shorter way of doing this:</p>

<p>dog dog dog</p>

<p>––––––––––––––––––––––––––</p>

<p>cat cat cat</p>

<p>––––––––––––––––––––––––––</p>

<p>hamster hamster hamster</p>

<p>Currently, I have to run 3 independent loops to group pet types together, and insert a line rule between them.</p>

<p><strong>How can I group pet types together and separate using a line rule?</strong></p>

<pre><code>&lt;div class="row"&gt;
      {% for pet in entries %}
            {% if pet.kind == 'dog' %}
                {% include '_partials/animalOrgChart' with {
                  title: pet.title,
                  subTitle: pet.jobTitle,
                  img: pet.headshot|length ? pet.headshot[0],                      
                  url: pet.url,
                  portrait: true
                } %}
            {% endif %}
      {% endfor %}
    &lt;hr class="line" /&gt; 
  &lt;/div&gt;
&lt;div class="row"&gt;
          {% for pet in entries %}
                {% if pet.kind == 'cat' %}
                    {% include '_partials/animalOrgChart' with {
                      title: pet.title,
                      subTitle: pet.jobTitle,
                      img: pet.headshot|length ? pet.headshot[0],                      
                      url: pet.url,
                      portrait: true
                    } %}
                {% endif %}
          {% endfor %}
        &lt;hr class="line" /&gt; 
   &lt;/div&gt;
&lt;div class="row"&gt;
          {% for pet in entries %}
                {% if pet.kind == 'hamster' %}
                    {% include '_partials/animalOrgChart' with {
                      title: pet.title,
                      subTitle: pet.jobTitle,
                      img: pet.headshot|length ? pet.headshot[0],                      
                      url: pet.url,
                      portrait: true
                    } %}
                {% endif %}
          {% endfor %}
        &lt;hr class="line" /&gt; 
   &lt;/div&gt;
</code></pre>

<loop>
Loop through all entry types, separate each group type by line rule (hr)
# Loop through all entry types, separate each group type by line rule (hr)

(Asked by: username_0 on 2018-05-17)

I'm looking for shorter way of doing this:
dog dog dog
––––––––––––––––––––––––––
cat cat cat
––––––––––––––––––––––––––
hamster hamster hamster
Currently, I have to run 3 independent loops to group pet types together, and insert a line rule between them.
How can I group pet types together and separate using a line rule?

      {% for pet in entries %}
            {% if pet.kind == 'dog' %}
                {% include '_partials/animalOrgChart' with {
                  title: pet.title,
                  subTitle: pet.jobTitle,
                  img: pet.headshot|length ? pet.headshot[0],                      
                  url: pet.url,
                  portrait: true
                } %}
            {% endif %}
      {% endfor %}
     
  

          {% for pet in entries %}
                {% if pet.kind == 'cat' %}
                    {% include '_partials/animalOrgChart' with {
                      title: pet.title,
                      subTitle: pet.jobTitle,
                      img: pet.headshot|length ? pet.headshot[0],                      
                      url: pet.url,
                      portrait: true
                    } %}
                {% endif %}
          {% endfor %}
         
   

          {% for pet in entries %}
                {% if pet.kind == 'hamster' %}
                    {% include '_partials/animalOrgChart' with {
                      title: pet.title,
                      subTitle: pet.jobTitle,
                      img: pet.headshot|length ? pet.headshot[0],                      
                      url: pet.url,
                      portrait: true
                    } %}
                {% endif %}
          {% endfor %}

**Tags:** loop

---

## 1 answers

### Answer by username_1 on 2018-05-17. Score: 3 | Accepted answer

You can use the group filter
{% set allEntries = craft.entries.section('pets').all() %}
{% set allEntriesByType = allEntries|group('type') %}
{% for type, pets in allEntriesByType %}

     All Pets of Type {{ type }}

     {% for pet in pets %}
         {% include '_partials/animalOrgChart' with {
             title: pet.title,
             subTitle: pet.jobTitle,
             img: pet.headshot|length ? pet.headshot[0],                      
             url: pet.url,
             portrait: true
         } %}        
    {% endfor %}

     
{% endfor %}

> **Comments:**
> **username_0**: This is great, so simple and concise. For {% set allEntriesByType = allEntries|group('type') %}, I'm targeting a field handle. It works great @Robin. Thanks again.  

---

