<p>Try adding square brackets around your object:</p>

<pre><code>{% set testsByAllergenGroup = testsByAllergenGroup|merge([{ (group.title): entries }]) %}
</code></pre>

<p><strong>Update:</strong></p>

<p>First thing I'd change is to set the object keys to something unique (slug or id works better than title).</p>

<p>To feed the object with all properties you need (so you don't need to do further DB queries later), do the following.</p>

<p>The values of your object can be objects too, this allows you to assign more data to each individual key.
You could define a custom key for each property you need for your output (i.e. <code>'groupId': group.id</code>) or
pass the whole category model (see example code below), which allows you to access all of its properties in the output loop.
With the entries you need to make sure to assign an array of entry models and not an ElementCriteriaModel! So make sure to
add the <code>find()</code> method to your <code>craft.entries</code> call.</p>

<pre><code>{% for group in groups %}
    {% set entries = entries.relatedTo(group).find() %}
    {% set testsByAllergenGroup = testsByAllergenGroup|merge([{
        (group.slug): {
            'group': group,
            'entries': entries
        }
    }]) %}
{% endfor %}

{% for key, value in testsByAllergenGroup %}
    {{ value.group.id }}: {{ value.group.title }}
    {% for entry in value.entries %}
        {{ entry.title }}
    {% endfor %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-03-03T00:16:54.850Z
This code causes an "Internal Server Error: Object of class Craft\ElementCriteriaModel could not be converted to string"
joshmedeski
CC BY-SA 3.0
2015-02-27T23:04:00.733Z
2015-03-03T08:29:20.653Z
2015-03-03T08:29:20.653Z
carlcs
<p>I've started off with this solution:
<a href="http://craftcookbook.net/recipes/330" rel="nofollow">http://craftcookbook.net/recipes/330</a></p>

<pre><code>{% set testsByAllergenGroup = {} %}
{% set entries = craft.entries.section('tests').order('title asc') %}
{% set groups  = craft.categories.group('allergenGroup') %}

{% for group in groups %}
  {% set entries = entries.relatedTo(group) %}
  {% if entries|length %}
    {% set testsByAllergenGroup = testsByAllergenGroup|merge({
      (group.title): entries
    }) %}
  {% endif %}
{% endfor %}

    {% if groups|length %}
      &lt;div class="row"&gt;
        &lt;div class="small-12 columns"&gt;
          &lt;ul class="inline-list"&gt;
            &lt;li&gt;Allergy Groups:&lt;/li&gt;
            {% for group in groups %}
              &lt;li&gt;&lt;a href="#{{ group.slug }}"&gt;{{ group.title }}&lt;/a&gt;&lt;/li&gt;
            {% endfor %}
          &lt;/ul&gt;
        &lt;/div&gt;
      &lt;/div&gt;

      {% for groupTitle, groupTests in testsByAllergenGroup %}
        &lt;h2 id="{{ groupTitle }}"  class="content__subtitle"&gt;
          {{ groupTitle }}
        &lt;/h2&gt;
        &lt;div class="content__panel"&gt;
          {% for entry in groupTests %}
            &lt;div class="test-menu-item"&gt;
              &lt;div class="row"&gt;
                &lt;div class="medium-7 columns"&gt;
                  &lt;h4&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/h4&gt;
                &lt;/div&gt;
                &lt;div class="medium-5 columns"&gt;
                  &lt;ul class="button-group radius even-2"&gt;
                    &lt;li&gt;&lt;a href="{{ entry.url }}" class="info tiny button"&gt;View Details&lt;/a&gt;&lt;/li&gt;
                    &lt;li&gt;&lt;a href="{{ entry.url }}" class="tiny button"&gt;Add to Cart ($40)&lt;/a&gt;&lt;/li&gt;
                  &lt;/ul&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;
          {% endfor %}
        &lt;/div&gt;
        &lt;p&gt;
          &lt;a href="#top" class="content__link--top"&gt;BACK TO TOP&lt;/a&gt;
        &lt;/p&gt;
      {% endfor %}

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

<p>I'm hoping to output the group title and slug, but this code will only allow for one variable to be set in the merge set:</p>

<pre><code>{% set testsByAllergenGroup = testsByAllergenGroup|merge({
  (group.title): entries
 }) %}
</code></pre>

<p>I'm hoping to do something like this:</p>

<pre><code>{% for group, groupTests in testsByAllergenGroup %}
  &lt;h2 id="{{ group.slug }}"  class="content__subtitle"&gt;
    {{ group.id }}
  &lt;/h2&gt;
</code></pre>

<p>Can you guys help me out? Thanks!</p>

<p>p.s. this is my first major Craft CMS project so Twig is new to me ;)</p>

<entries><categories>
Grouping by Category with Multiple Keys
# Grouping by Category with Multiple Keys

(Asked by: username_0 on 2015-02-27)

I've started off with this solution:
http://craftcookbook.net/recipes/330
{% set testsByAllergenGroup = {} %}
{% set entries = craft.entries.section('tests').order('title asc') %}
{% set groups  = craft.categories.group('allergenGroup') %}

{% for group in groups %}
  {% set entries = entries.relatedTo(group) %}
  {% if entries|length %}
    {% set testsByAllergenGroup = testsByAllergenGroup|merge({
      (group.title): entries
    }) %}
  {% endif %}
{% endfor %}

    {% if groups|length %}
      
        
          
            Allergy Groups:
            {% for group in groups %}
              {{ group.title }}
            {% endfor %}
          
        
      

      {% for groupTitle, groupTests in testsByAllergenGroup %}
        
          {{ groupTitle }}
        
        
          {% for entry in groupTests %}
            
              
                
                  {{ entry.title }}
                
                
                  
                    View Details
                    Add to Cart ($40)
                  
                
              
            
          {% endfor %}
        
        
          BACK TO TOP
        
      {% endfor %}

    {% endif %}

I'm hoping to output the group title and slug, but this code will only allow for one variable to be set in the merge set:
{% set testsByAllergenGroup = testsByAllergenGroup|merge({
  (group.title): entries
 }) %}

I'm hoping to do something like this:
{% for group, groupTests in testsByAllergenGroup %}
  
    {{ group.id }}
  

Can you guys help me out? Thanks!
p.s. this is my first major Craft CMS project so Twig is new to me ;)

**Tags:** entries, categories


---

## 1 answers

### Answer by username_1 on 2015-02-27. Score: 1

Try adding square brackets around your object:
{% set testsByAllergenGroup = testsByAllergenGroup|merge([{ (group.title): entries }]) %}

Update:
First thing I'd change is to set the object keys to something unique (slug or id works better than title).
To feed the object with all properties you need (so you don't need to do further DB queries later), do the following.
The values of your object can be objects too, this allows you to assign more data to each individual key.
You could define a custom key for each property you need for your output (i.e. 'groupId': group.id) or
pass the whole category model (see example code below), which allows you to access all of its properties in the output loop.
With the entries you need to make sure to assign an array of entry models and not an ElementCriteriaModel! So make sure to
add the find() method to your craft.entries call.
{% for group in groups %}
    {% set entries = entries.relatedTo(group).find() %}
    {% set testsByAllergenGroup = testsByAllergenGroup|merge([{
        (group.slug): {
            'group': group,
            'entries': entries
        }
    }]) %}
{% endfor %}

{% for key, value in testsByAllergenGroup %}
    {{ value.group.id }}: {{ value.group.title }}
    {% for entry in value.entries %}
        {{ entry.title }}
    {% endfor %}
{% endfor %}

> **Comments:**
> **username_0**: This code causes an "Internal Server Error: Object of class Craft\ElementCriteriaModel could not be converted to string"  

---

