1

I need to output a row every three items in a matrix. Here is what I have so far. The problem is that this outputs a row every three items but it duplicates each matrix item three times and puts them in a row rather than taking the items as they come. Any help here would be very much appreciated Thanks.

{% set aboutInfo = entry.imageLinkBoxes %}
{% set infoGrouped = aboutInfo|batch(3) %}

{% for group in infoGrouped %}
    {% for block in entry.imageLinkBoxes %}

        {% if block.type == "imageLinkBox" %}

            {% set row = 'row--' ~ loop.index %}

            <div class="row {{ row }}">
                {% for entry in group %}

                    <div class="span4">
                        <div class="icon-box-2">    
                            <i class="ifc-tv_show"></i>
                            <div class="icon-box-content">
                                <h3 class="text-uppercase text-center">
                                    <a href="#">{{ block.entry.first().title }}</a>
                                </h3>
                                <p>{{ block.text }}</p>
                            </div>
                        </div>               
                    </div>

                {% endfor %}
            </div>

        {% endif %}

    {% endfor %}
{% endfor %}
  • 1
    Your code is a little bit confusing, but your issue is with the nested loops and where you put certain parts of the code. Can you describe your Matrix field content? What, exactly are the items you want to put in each .row? Are they entries inside Matrix blocks? – Mats Mikkel Rummelhoff Oct 17 '15 at 16:45
4

To my understanding this should work for a matrix field (called imageLinkBoxes) that contains only one type of block (imageLinkBox) that has 3 fields (imageLinkBox.linkText, imageLinkBox.linkUrl and imageLinkBox.text)

{% set aboutInfo = entry.imageLinkBoxes %} {# get all the blocks in the matrix #}

{% if aboutInfo|length %} {# check we have at least one block in the matrix #}

    {% set infoGrouped = aboutInfo|batch(3) %} {# group them in groups of 3 blocks #}

    {% for group in infoGrouped %}  {# loop through all the groups #}

        {% set row = 'row--' ~ loop.index %} {# set the class #}

        <div class="row {{ row }}"> {# open the row #}

        {% for block in group %} {# loop through all the blocks in current group #}

            <div class="span4">
                <div class="icon-box-2">    
                    <i class="ifc-tv_show"></i>
                    <div class="icon-box-content">
                        <h3 class="text-uppercase text-center">
                            <a href="{{ block.linkUrl }}">{{ block.linkText }}</a> 
                        </h3>
                        <p>{{ block.text }}</p>
                    </div>  
                </div>
               <!-- /.icon-box-2 -->               
            </div>
            <!-- /.span4-->

        {% endfor %}

        </div> {# close the row #}
        <!-- /.row-->

    {% endfor %}

{% endif %}

If for some reason you have more than one type of block in the matrix, then you would need to do a change:

Replace:

{% set aboutInfo = entry.imageLinkBoxes %}

With:

{% set aboutInfo = entry.imageLinkBoxes.type('imageLinkBox') %}

That would filter the blocks to only those of the type imageLinkBox.

| improve this answer | |

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.