Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-03T00:16:54.850Z
Answers.Text: This code causes an "Internal Server Error: Object of class Craft\ElementCriteriaModel could not be converted to string"
Answers.UserDisplayName: joshmedeski
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-27T23:04:00.733Z
Answers.LastActivityDate: 2015-03-03T08:29:20.653Z
Answers.LastEditDate: 2015-03-03T08:29:20.653Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Tags: <entries><categories>
Title: Grouping by Category with Multiple Keys
ThreadText: # 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 %}
        <div class="row">
          <div class="small-12 columns">
            <ul class="inline-list">
              <li>Allergy Groups:</li>
              {% for group in groups %}
                <li><a href="#{{ group.slug }}">{{ group.title }}</a></li>
              {% endfor %}
            </ul>
          </div>
        </div>
  
        {% for groupTitle, groupTests in testsByAllergenGroup %}
          <h2 id="{{ groupTitle }}"  class="content__subtitle">
            {{ groupTitle }}
          </h2>
          <div class="content__panel">
            {% for entry in groupTests %}
              <div class="test-menu-item">
                <div class="row">
                  <div class="medium-7 columns">
                    <h4><a href="{{ entry.url }}">{{ entry.title }}</a></h4>
                  </div>
                  <div class="medium-5 columns">
                    <ul class="button-group radius even-2">
                      <li><a href="{{ entry.url }}" class="info tiny button">View Details</a></li>
                      <li><a href="{{ entry.url }}" class="tiny button">Add to Cart ($40)</a></li>
                    </ul>
                  </div>
                </div>
              </div>
            {% endfor %}
          </div>
          <p>
            <a href="#top" class="content__link--top">BACK TO TOP</a>
          </p>
        {% 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 %}
    <h2 id="{{ group.slug }}"  class="content__subtitle">
      {{ group.id }}
    </h2>
  
  ```
  
  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"  
  
  ---
  
  