2

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 ;)

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 %}
| improve this answer | |
  • This code causes an "Internal Server Error: Object of class Craft\ElementCriteriaModel could not be converted to string" – joshmedeski Mar 3 '15 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.