<p>The way you describe the output you want (3 blockquotes followed by a list of 3 images), you are going to need to loop over the testimonials twice. Here is a solution detects the first of the testimonials blocks and starts a secondary loop for the blockquotes. You will need the <a href="https://github.com/marionnewlevant/craft-mnbreakandcontinue" rel="nofollow">break and continue</a> plugin to exit out of that inner loop.</p>

<pre><code>{% for block in entry.matrixField %}
  {% switch block.type %}
    {% case 'testimonials' %}

      {# is this the first or last block ? #}
      {% set isFirst = not block.getPrev() or block.getPrev().type != 'testimonials' %}
      {% set isLast = not block.getNext() or block.getNext().type != 'testimonials' %}

      {% if isFirst %}
        {# we are going to fake a loop for the blockquotes #}
        {% set currentBlock = block %}
        {# twig has no while loop, so... #}
        {% for x in [1..1000] %}

          {% if not currentBlock or currentBlock.type != 'testimonials' %}
            {% break %} {# from the breakandcontinue plugin #}
          {% endif %}

          &lt;blockquote class="blockquote-unstyled" style="display:none;"&gt;
            &lt;footer class="text-uppercase"&gt;
              &lt;span title="Source Title"&gt;{{currentBlock.sourceTitle}}&lt;/span&gt;
            &lt;/footer&gt;
            &lt;p class="lead"&gt;“{{currentBlock.lead}}”&lt;/p&gt;
          &lt;/blockquote&gt;

          {# on to the next #}
          {% set currentBlock = currentBlock.getNext() %}
        {% endfor %}

        {# first one, so open the image list #}
        &lt;ul class="list-inline"&gt;
      {% endif %}

      {# Output image for this testimonial here #}
      {% set image = block.image.first() %}
      &lt;li&gt;&lt;img class="img-responsive img-circle" alt="Profile" src="{{ image.getUrl('testimonials') }}" width="{{ image.getWidth('testimonials') }}" height="{{ image.getHeight('testimonials') }}"/&gt;&lt;/li&gt;

      {% if isLast %} {# last one, close the list #}
        &lt;/ul&gt;
      {% endif %}
    {# other cases here... #}
  {% endswitch %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-05-13T00:05:52.017Z
Worked great was a typo in your {% if not currentBlock or currentBlock.type != testimonials' %} opening single quote on testimonials. But was quickly spotted.
Bowenac
CC BY-SA 3.0
2015-05-13T01:03:10.903Z
Glad it worked! Fixed the typo for the next person.
Marion Newlevant
CC BY-SA 3.0
2015-05-12T17:11:58.197Z
2015-05-13T01:02:14.177Z
2015-05-13T01:02:14.177Z
Marion Newlevant
<p>I think you're going to drive yourself crazy with all the if then statements ;) — I might suggest you try a different approach to make this a bit more manageable. I can think of a few options.</p>

<p><strong>Option 1</strong>. Create a 'testimonials' matrix field and a 'layout' matrix field. The 'testimonials' matrix can be used to organize the testimonials, and the 'layout' matrix can be used to define the page layout order — you can then simply include the testimonials by adding a 'dummy' testimonials blockType to the layout matrix with no content in it.</p>

<pre><code>{% for block in entry.layout %}
    {% switch entry.layout %}

        {% case 'testimonials'%}

            &lt;div class="container-fluid add-top"&gt;
                &lt;div class="row"&gt;
                    &lt;div class="text-center testimonials"&gt;

                        {% set imageHTML = '' %}
                        {% for block in entry.testimonials %}

                            &lt;blockquote class="blockquote-unstyled" style="display:none;"&gt;
                                &lt;footer class="text-uppercase"&gt;
                                    &lt;span title="Source Title"&gt;{{block.sourceTitle}}&lt;/span&gt;
                                &lt;/footer&gt;
                                &lt;p class="lead"&gt;“{{block.lead}}”&lt;/p&gt;
                            &lt;/blockquote&gt;

                            {% set image = block.image.first() %}
                            {% set imageHTML = imageHTML ~ '&lt;li&gt;&lt;img class="img-responsive img-circle" alt="Profile" src="%url%" width="%width%" height="%height%"/&gt;&lt;/li&gt;'|replace({
                                %url%: image.getUrl('testimonials'),
                                %height%: image.getWidth('testimonials'), 
                                %width%: image.getHeight('testimonials')    
                            }) %}

                        {% endfor %}

                        {% if imageHTML %}
                            {{ imageHTML }}
                        {% endif %}

                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;

        {% case 'someOtherBlock' %}

            {# output other blocks as needed #}

    {% endswitch %}
{% endfor %}
</code></pre>

<p><strong>Option 2</strong>. Something else to consider is to use the 'superTable' plugin, which if I understand correctly, can be placed inside a matrix field. You can then loop through the matrix (i.e. 'layout') using switch, and then loop through the 'testimonials' inside the table. Unlike the solution above this would allow for multiple testimonial areas on your page, if needed.</p>

<p><strong>Option 3</strong>. You might also consider creating a separate channel for your 'testimonials', and then use an entries fieldType to include multiple testimonials in your testimonials blocktype. While slightly more difficult for data entry, the advantage would be that you could reuse your testimonials on multiple pages. This would also allow for multiple testimonial areas on the same page.</p>

CC BY-SA 3.0
2015-05-12T05:43:33.903Z
Any field type you can recommend to place in a dummy matrix block type? It doesn't work without any, right? And I think Super Table only works the other way round, Super Table inside Matrix field.
carlcs
CC BY-SA 3.0
2015-05-12T05:47:34.843Z
Not sure you need any fields — but if so a 'lightswitch' would work nicely. Regarding the superTable inside Matrix... you would certainly know better! I'll update the answer. Thanks @carlcs!
Douglas McDonald
CC BY-SA 3.0
2015-05-12T06:34:22.223Z
Or a read-only field type like [fieldnotes](http://michaelrog.com/craft/lagniappe/fieldnotes). Haven't tested that though.
carlcs
CC BY-SA 3.0
2015-05-12T12:01:11.327Z
Thanks for the suggestion but I don't think that will workout as some pages might need multiple testimonial areas. If I was to do that I would only be able to use it once.
Bowenac
CC BY-SA 3.0
2015-05-12T15:48:21.277Z
Understood. I would seriously consider option 2 then, using the superTable plugin, which should be ideal for this. Yet another option would be to create another channel with testimonial snippets, that you can add to an entries field (i.e. 'testimonials') in one of your matrix blocks. Although a bit more difficult for data entry, the advantage is that you could reuse the same testimonial on several pages.
Douglas McDonald
CC BY-SA 3.0
2015-05-12T05:37:16.473Z
2015-05-12T15:58:32.973Z
2015-05-12T15:58:32.973Z
Douglas McDonald
<p>Anyone know if it's possible to check what the current field is while in a loop of a matrix block? Running into an issue with a matrix block in my loop for how I want to output the content.</p>

<pre><code>{% case 'testimonials' %}
{% if block.getPrev() %}
    {# if the previous block isn't the "testimonials" block start container #}
    {% if block.getPrev().type != 'testimonials' %}
    &lt;div class="container-fluid add-top"&gt;
        &lt;div class="row"&gt;
            &lt;div class="text-center testimonials"&gt;
    {% endif %}
    {# if there isn't a previous block start a div could be the first block#}
    {% else %}
    &lt;div class="container-fluid add-top"&gt;
        &lt;div class="row"&gt;
            &lt;div class="text-center testimonials"&gt;
{% endif %}

            {# Output Content #}
            {# Output each testimonial here #}
            &lt;blockquote class="blockquote-unstyled" style="display:none;"&gt;
                &lt;footer class="text-uppercase"&gt;
                    &lt;span title="Source Title"&gt;{{block.sourceTitle}}&lt;/span&gt;
                &lt;/footer&gt;
                &lt;p class="lead"&gt;“{{block.lead}}”&lt;/p&gt;
            &lt;/blockquote&gt;

            {% if block.getPrev().type != 'testimonials' %}
            &lt;ul class="list-inline"&gt;
            {% endif %}

            {# Output image for each testimonial here #}
            {% set image = block.image.first() %}
            &lt;li&gt;&lt;img class="img-responsive img-circle" alt="Profile" src="{{ image.getUrl('testimonials') }}" width="{{ image.getWidth('testimonials') }}" height="{{ image.getHeight('testimonials') }}"/&gt;&lt;/li&gt;

            {% if block.getNext().type != 'testimonials' %}
            &lt;/ul&gt;
            {% endif %}
            {# Output Content #}

{# if this is the "testimonials" block type #}
{% if block.type == 'testimonials' %}
    {# if there is another block to check #}
    {% if block.getNext() %}

    {# if the next block isn't the "testimonials" block end div #}
    {% if block.getNext().type != 'testimonials' %}
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    {% endif %}

    {# if there isn't another block end a ul might be the last block#}
    {% else %}
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    {% endif %}
{% endif %}
&lt;!-- End Testimonials Block --&gt;
</code></pre>

<p>So if I enter three testimonials I want the content to appear like this.</p>

<pre><code>&lt;blockquote class="blockquote-unstyled" style="display:none;"&gt;
    &lt;footer class="text-uppercase"&gt;
        &lt;span title="Source Title"&gt;{{block.sourceTitle}}&lt;/span&gt;
    &lt;/footer&gt;
    &lt;p class="lead"&gt;“{{block.lead}}”&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote class="blockquote-unstyled" style="display:none;"&gt;
    &lt;footer class="text-uppercase"&gt;
        &lt;span title="Source Title"&gt;{{block.sourceTitle}}&lt;/span&gt;
    &lt;/footer&gt;
    &lt;p class="lead"&gt;“{{block.lead}}”&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote class="blockquote-unstyled" style="display:none;"&gt;
    &lt;footer class="text-uppercase"&gt;
        &lt;span title="Source Title"&gt;{{block.sourceTitle}}&lt;/span&gt;
    &lt;/footer&gt;
    &lt;p class="lead"&gt;“{{block.lead}}”&lt;/p&gt;
&lt;/blockquote&gt;
&lt;ul class="list-inline"&gt;
&lt;li&gt;&lt;img class="img-responsive img-circle" alt="Profile" src="{{ image.getUrl('testimonials') }}" width="{{ image.getWidth('testimonials') }}" height="{{ image.getHeight('testimonials') }}"/&gt;&lt;/li&gt;
&lt;li&gt;&lt;img class="img-responsive img-circle" alt="Profile" src="{{ image.getUrl('testimonials') }}" width="{{ image.getWidth('testimonials') }}" height="{{ image.getHeight('testimonials') }}"/&gt;&lt;/li&gt;
&lt;li&gt;&lt;img class="img-responsive img-circle" alt="Profile" src="{{ image.getUrl('testimonials') }}" width="{{ image.getWidth('testimonials') }}" height="{{ image.getHeight('testimonials') }}"/&gt;&lt;/li&gt;
&lt;/ul&gt;
</code></pre>

<p>Before changing to use a switch I was using the following which worked great, just can't think of an equivalent for switch.</p>

<pre><code>&lt;!-- Testimonials Block --&gt;
{% if entry.testimonials|length %}
&lt;div class="container-fluid add-top"&gt;
    &lt;div class="row"&gt;
        &lt;div class="text-center testimonials"&gt;
        {% for block in entry.testimonials %}
            {% if block.type == "testimonial" %}
                {% set image = block.image.first() %}
                &lt;blockquote class="blockquote-unstyled" style="display:none;"&gt;
                    &lt;footer class="text-uppercase"&gt;
                        &lt;span title="Source Title"&gt;{{block.sourceTitle}}&lt;/span&gt;
                    &lt;/footer&gt;
                    &lt;p class="lead"&gt;“{{block.lead}}”&lt;/p&gt;
                &lt;/blockquote&gt;
            {% endif %}
        {% endfor %} 
        &lt;div class="list-inline"&gt;
        {% for block in entry.testimonials %}
            {% if block.type == "testimonial" %}
                {% set image = block.image.first() %}
                    &lt;li&gt;&lt;img class="img-responsive img-circle" alt="Profile" src="{{ image.getUrl('testimonials') }}" width="{{ image.getWidth('testimonials') }}" height="{{ image.getHeight('testimonials') }}"/&gt;&lt;/li&gt;
            {% endif %}
        {% endfor %} 
        &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
{% endif %}
</code></pre>

<templating><matrix><field><forloop>
Matrix loop nesting issue using switch
# Matrix loop nesting issue using switch

(Asked by: username_0 on 2015-05-12)

Anyone know if it's possible to check what the current field is while in a loop of a matrix block? Running into an issue with a matrix block in my loop for how I want to output the content.
{% case 'testimonials' %}
{% if block.getPrev() %}
    {# if the previous block isn't the "testimonials" block start container #}
    {% if block.getPrev().type != 'testimonials' %}
    
        
            
    {% endif %}
    {# if there isn't a previous block start a div could be the first block#}
    {% else %}
    
        
            
{% endif %}

            {# Output Content #}
            {# Output each testimonial here #}
            
                
                    {{block.sourceTitle}}
                
                “{{block.lead}}”
            

            {% if block.getPrev().type != 'testimonials' %}
            
            {% endif %}

            {# Output image for each testimonial here #}
            {% set image = block.image.first() %}
            

            {% if block.getNext().type != 'testimonials' %}
            
            {% endif %}
            {# Output Content #}

{# if this is the "testimonials" block type #}
{% if block.type == 'testimonials' %}
    {# if there is another block to check #}
    {% if block.getNext() %}

    {# if the next block isn't the "testimonials" block end div #}
    {% if block.getNext().type != 'testimonials' %}
            
        
    
    {% endif %}

    {# if there isn't another block end a ul might be the last block#}
    {% else %}
            
        
    
    {% endif %}
{% endif %}


So if I enter three testimonials I want the content to appear like this.

    
        {{block.sourceTitle}}
    
    “{{block.lead}}”


    
        {{block.sourceTitle}}
    
    “{{block.lead}}”


    
        {{block.sourceTitle}}
    
    “{{block.lead}}”







Before changing to use a switch I was using the following which worked great, just can't think of an equivalent for switch.

{% if entry.testimonials|length %}

    
        
        {% for block in entry.testimonials %}
            {% if block.type == "testimonial" %}
                {% set image = block.image.first() %}
                
                    
                        {{block.sourceTitle}}
                    
                    “{{block.lead}}”
                
            {% endif %}
        {% endfor %} 
        
        {% for block in entry.testimonials %}
            {% if block.type == "testimonial" %}
                {% set image = block.image.first() %}
                    
            {% endif %}
        {% endfor %} 
        
        
    

{% endif %}

**Tags:** templating, matrix, field, forloop


---

## 2 answers

### Answer by username_1 on 2015-05-12. Score: 1 | Accepted answer

The way you describe the output you want (3 blockquotes followed by a list of 3 images), you are going to need to loop over the testimonials twice. Here is a solution detects the first of the testimonials blocks and starts a secondary loop for the blockquotes. You will need the break and continue plugin to exit out of that inner loop.
{% for block in entry.matrixField %}
  {% switch block.type %}
    {% case 'testimonials' %}

      {# is this the first or last block ? #}
      {% set isFirst = not block.getPrev() or block.getPrev().type != 'testimonials' %}
      {% set isLast = not block.getNext() or block.getNext().type != 'testimonials' %}

      {% if isFirst %}
        {# we are going to fake a loop for the blockquotes #}
        {% set currentBlock = block %}
        {# twig has no while loop, so... #}
        {% for x in [1..1000] %}

          {% if not currentBlock or currentBlock.type != 'testimonials' %}
            {% break %} {# from the breakandcontinue plugin #}
          {% endif %}

          
            
              {{currentBlock.sourceTitle}}
            
            “{{currentBlock.lead}}”
          

          {# on to the next #}
          {% set currentBlock = currentBlock.getNext() %}
        {% endfor %}

        {# first one, so open the image list #}
        
      {% endif %}

      {# Output image for this testimonial here #}
      {% set image = block.image.first() %}
      

      {% if isLast %} {# last one, close the list #}
        
      {% endif %}
    {# other cases here... #}
  {% endswitch %}
{% endfor %}

> **Comments:**
> **username_0**: Worked great was a typo in your {% if not currentBlock or currentBlock.type != testimonials' %} opening single quote on testimonials. But was quickly spotted.  
> **username_1**: Glad it worked! Fixed the typo for the next person.  

---

### Answer by username_2 on 2015-05-12. Score: 2

I think you're going to drive yourself crazy with all the if then statements ;) — I might suggest you try a different approach to make this a bit more manageable. I can think of a few options.
Option 1. Create a 'testimonials' matrix field and a 'layout' matrix field. The 'testimonials' matrix can be used to organize the testimonials, and the 'layout' matrix can be used to define the page layout order — you can then simply include the testimonials by adding a 'dummy' testimonials blockType to the layout matrix with no content in it.
{% for block in entry.layout %}
    {% switch entry.layout %}

        {% case 'testimonials'%}

            
                
                    

                        {% set imageHTML = '' %}
                        {% for block in entry.testimonials %}

                            
                                
                                    {{block.sourceTitle}}
                                
                                “{{block.lead}}”
                            

                            {% set image = block.image.first() %}
                            {% set imageHTML = imageHTML ~ ''|replace({
                                %url%: image.getUrl('testimonials'),
                                %height%: image.getWidth('testimonials'), 
                                %width%: image.getHeight('testimonials')    
                            }) %}

                        {% endfor %}

                        {% if imageHTML %}
                            {{ imageHTML }}
                        {% endif %}

                    
                
            

        {% case 'someOtherBlock' %}

            {# output other blocks as needed #}

    {% endswitch %}
{% endfor %}

Option 2. Something else to consider is to use the 'superTable' plugin, which if I understand correctly, can be placed inside a matrix field. You can then loop through the matrix (i.e. 'layout') using switch, and then loop through the 'testimonials' inside the table. Unlike the solution above this would allow for multiple testimonial areas on your page, if needed.
Option 3. You might also consider creating a separate channel for your 'testimonials', and then use an entries fieldType to include multiple testimonials in your testimonials blocktype. While slightly more difficult for data entry, the advantage would be that you could reuse your testimonials on multiple pages. This would also allow for multiple testimonial areas on the same page.

> **Comments:**
> **username_3**: Any field type you can recommend to place in a dummy matrix block type? It doesn't work without any, right? And I think Super Table only works the other way round, Super Table inside Matrix field.  
> **username_2**: Not sure you need any fields — but if so a 'lightswitch' would work nicely. Regarding the superTable inside Matrix... you would certainly know better! I'll update the answer. Thanks @username_3!  
> **username_3**: Or a read-only field type like [fieldnotes](http://michaelrog.com/craft/lagniappe/fieldnotes). Haven't tested that though.  
> **username_0**: Thanks for the suggestion but I don't think that will workout as some pages might need multiple testimonial areas. If I was to do that I would only be able to use it once.  
> **username_2**: Understood. I would seriously consider option 2 then, using the superTable plugin, which should be ideal for this. Yet another option would be to create another channel with testimonial snippets, that you can add to an entries field (i.e. 'testimonials') in one of your matrix blocks. Although a bit more difficult for data entry, the advantage is that you could reuse the same testimonial on several pages.  

---

