<p>Without understanding your use case or having the context of your full template and/or Matrix loop, it's very difficult to provide a good answer.</p>

<p>If you only want to "wrap all outputted blocks" in a container, it's as easy as putting the Matrix loop in a container, e.g.:</p>

<pre><code>&lt;div class="wrapper"&gt;
    {% for block in entry.myMatrixFieldHandle %}
        {% switch block.type %}
            {% case 'artworkText' %}
                ...
            {% case 'artworkImageSingle' %}
                ...
            {% case 'artworkImageDouble' %}
                ...
            {% case 'artworkFile' %}
            {% for asset in block.downloadUrl %}
                &lt;a href="{{ asset.getUrl() }}" target="_blank"&gt;
                    &lt;div class="file {{ block.platform }} gutter-bottom--half"&gt;&lt;/div&gt;
                &lt;/a&gt;
            {% endfor %}
        {% endswitch %}
    {% endfor %}
&lt;/div&gt;
</code></pre>

<p>Based on your comments, however, I suspect that what you really want is to only wrap the markup for all your <code>artworkFile</code> blocks.</p>

<p>If that is the case, you have to consider the fact that if you want to wrap all of the <code>artworkFile</code> blocks in a single container and not the other block types, then these <code>artworkFile</code> blocks will necessarily be removed from the general flow/chronology of your overall Matrix field. This may be exactly what you want, or a side effect you haven't considered - just wanted to mention it. (It also means that you could actually just move the <code>artworkFile</code> blocks to a separate Matrix field altogether, if that'd make any of this easier).</p>

<p>Anyway, there's at least a couple of ways to achieve the above with your current setup. </p>

<p>First, you can opt to remove the <code>artworkFile</code> switch case from your current Matrix loop, and create a secondary loop which only loops on <code>artworkFile</code> blocks somewhere else in your template. This secondary loop is then wrapped in a <code>&lt;div class="wrapper"&gt;</code> container, e.g.:</p>

<pre><code>&lt;div class="wrapper"&gt;
    {% for block in entry.myMatrixFieldHandle.type('artworkFile') %}
        {% for asset in block.downloadUrl %}
        &lt;a href="{{ asset.getUrl() }}" target="_blank"&gt;
            &lt;div class="file {{ block.platform }} gutter-bottom--half"&gt;&lt;/div&gt;
        &lt;/a&gt;
        {% endfor %}
    {% endfor %}
&lt;/div&gt;
</code></pre>

<p>Another option is to leave the <code>artworkFile</code> switch in your current Matrix loop, but instead of outputting the markup directly, you can cache it in an array – this is done by first putting the markup in a variable by wrapping the block markup in a <code>{% set %}</code> tag pair, adding that variable to an array declared outside the loop, and finally print the collected markup by joining the contents of that array inside a container, e.g.:</p>

<pre><code>{# Create an empty array outside the loop #}
{% set artworkFileBlocks = [] %}
{% for block in entry.myMatrixFieldHandle %}
    {% switch block.type %}
        {% case 'artworkText' %}
            ...
        {% case 'artworkImageSingle' %}
            ...
        {% case 'artworkImageDouble' %}
            ...
        {% case 'artworkFile' %}
        {# Cache the markup for the artworkFile block to a variable #}
        {% set artworkFileBlock %}
            {% for asset in block.downloadUrl %}
                &lt;a href="{{ asset.getUrl() }}" target="_blank"&gt;
                    &lt;div class="file {{ block.platform }} gutter-bottom--half"&gt;&lt;/div&gt;
                &lt;/a&gt;
            {% endfor %}
        {% endset %}
        {# Add the single artworkFile block markup to the array #}
        {% set artworkFileBlocks = artworkFileBlocks|merge([artworkFileBlock]) %}
    {% endswitch %}
{% endfor %}

{# Echo all artworkFile markup in a common container, whereever you need it #}
&lt;div class="wrapper"&gt;
    {{ artworkFileBlocks|join('')|raw }}
&lt;/div&gt;
</code></pre>

CC BY-SA 3.0
2015-11-22T10:30:37.603Z
Thanks for such a detailed answer. I wanted to keep the artworkFile block within the Matrix so it can be moved around within the rest of the content.
sarah3585
CC BY-SA 3.0
2015-11-22T12:07:49.983Z
Right, that's a valid reason to keep all the block types in a single Matrix field, even if the `artworkFile` blocks aren't output with the other block types.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-11-21T19:20:08.783Z
2015-11-21T19:20:08.783Z
Mats Mikkel Rummelhoff
<p>If you just want to affect the <code>artworkFile</code> blocks, you could do this:</p>

<pre><code>{% case "artworkFile" %}
{% for asset in block.downloadUrl %}
    {% if loop.first %}&lt;div class="wrapper"&gt;{% endif %}
    &lt;a href="{{ asset.getUrl() }}" target="_blank"&gt;
    &lt;div class="file {{ block.platform }} gutter-bottom--half"&gt;&lt;/div&gt;&lt;/a&gt;
    {% if loop.last %}&lt;/div&gt;{% endif %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-11-20T16:28:06.937Z
Thanks. This seems to wrap all individual fields rather than all. I've updated my questions with more information.
sarah3585
CC BY-SA 3.0
2015-11-17T14:39:28.927Z
2015-11-17T14:39:28.927Z
Steve Rowling
<p>I'm outputting fields from a Matrix block, how do I then wrap all outputted fields from 'artworkFile' that are available with some HTML. Not individually wrapped, but all outputted fields.</p>

<p>Current Code:</p>

<pre><code>{% case "artworkFile" %}
{% for asset in block.downloadUrl %}
 &lt;a href="{{ asset.getUrl() }}" target="_blank"&gt;
 &lt;div class="file {{ block.platform }} gutter-bottom--half"&gt;&lt;/div&gt;&lt;/a&gt;
{% endfor %}
</code></pre>

<p>Desired Output</p>

<pre><code>&lt;div class="wrapper"&gt;
  &lt;a href="asseturl1.png&gt;
   &lt;div class="file exampleplatform1 gutter-bottom--half"&gt;&lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="asseturl2.png&gt;
   &lt;div class="file exampleplatform2 gutter-bottom--half"&gt;&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;
</code></pre>

<p>Matrix Setup
<a href="https://i.sstatic.net/jAPs1.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/jAPs1.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2015-11-16T11:56:26.923Z
The use case is missing here, which makes your question hard to answer – you might want to include more of your current template code. Is there are reason why you can't just wrap the Matrix loop in a container, i.e. `<div class="wrapper">{% for block in myMatrixField %}...{% endfor %}</div>`?
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-11-20T16:23:34.607Z
That seems to wrap each block in a 'wrapper' I want it to wrap all outputted blocks. I will update my question for more information.
sarah3585
<matrix><loop><batch>
Wrapping a matrix block
# Wrapping a matrix block

(Asked by: username_0 on 2015-11-16)

I'm outputting fields from a Matrix block, how do I then wrap all outputted fields from 'artworkFile' that are available with some HTML. Not individually wrapped, but all outputted fields.
Current Code:
{% case "artworkFile" %}
{% for asset in block.downloadUrl %}
 
 
{% endfor %}

Desired Output

  
  
  
  


Matrix Setup

**Tags:** matrix, loop, batch

> **Comments:**
> **username_1**: The use case is missing here, which makes your question hard to answer – you might want to include more of your current template code. Is there are reason why you can't just wrap the Matrix loop in a container, i.e. `<div class="wrapper">{% for block in myMatrixField %}...{% endfor %}</div>`?  
> **username_0**: That seems to wrap each block in a 'wrapper' I want it to wrap all outputted blocks. I will update my question for more information.  

---

## 2 answers

### Answer by username_1 on 2015-11-21. Score: 3

Without understanding your use case or having the context of your full template and/or Matrix loop, it's very difficult to provide a good answer.
If you only want to "wrap all outputted blocks" in a container, it's as easy as putting the Matrix loop in a container, e.g.:

    {% for block in entry.myMatrixFieldHandle %}
        {% switch block.type %}
            {% case 'artworkText' %}
                ...
            {% case 'artworkImageSingle' %}
                ...
            {% case 'artworkImageDouble' %}
                ...
            {% case 'artworkFile' %}
            {% for asset in block.downloadUrl %}
                
                    
                
            {% endfor %}
        {% endswitch %}
    {% endfor %}


Based on your comments, however, I suspect that what you really want is to only wrap the markup for all your artworkFile blocks.
If that is the case, you have to consider the fact that if you want to wrap all of the artworkFile blocks in a single container and not the other block types, then these artworkFile blocks will necessarily be removed from the general flow/chronology of your overall Matrix field. This may be exactly what you want, or a side effect you haven't considered - just wanted to mention it. (It also means that you could actually just move the artworkFile blocks to a separate Matrix field altogether, if that'd make any of this easier).
Anyway, there's at least a couple of ways to achieve the above with your current setup. 
First, you can opt to remove the artworkFile switch case from your current Matrix loop, and create a secondary loop which only loops on artworkFile blocks somewhere else in your template. This secondary loop is then wrapped in a  container, e.g.:

    {% for block in entry.myMatrixFieldHandle.type('artworkFile') %}
        {% for asset in block.downloadUrl %}
        
            
        
        {% endfor %}
    {% endfor %}


Another option is to leave the artworkFile switch in your current Matrix loop, but instead of outputting the markup directly, you can cache it in an array – this is done by first putting the markup in a variable by wrapping the block markup in a {% set %} tag pair, adding that variable to an array declared outside the loop, and finally print the collected markup by joining the contents of that array inside a container, e.g.:
{# Create an empty array outside the loop #}
{% set artworkFileBlocks = [] %}
{% for block in entry.myMatrixFieldHandle %}
    {% switch block.type %}
        {% case 'artworkText' %}
            ...
        {% case 'artworkImageSingle' %}
            ...
        {% case 'artworkImageDouble' %}
            ...
        {% case 'artworkFile' %}
        {# Cache the markup for the artworkFile block to a variable #}
        {% set artworkFileBlock %}
            {% for asset in block.downloadUrl %}
                
                    
                
            {% endfor %}
        {% endset %}
        {# Add the single artworkFile block markup to the array #}
        {% set artworkFileBlocks = artworkFileBlocks|merge([artworkFileBlock]) %}
    {% endswitch %}
{% endfor %}

{# Echo all artworkFile markup in a common container, whereever you need it #}

    {{ artworkFileBlocks|join('')|raw }}

> **Comments:**
> **username_0**: Thanks for such a detailed answer. I wanted to keep the artworkFile block within the Matrix so it can be moved around within the rest of the content.  
> **username_1**: Right, that's a valid reason to keep all the block types in a single Matrix field, even if the `artworkFile` blocks aren't output with the other block types.  

---

### Answer by username_2 on 2015-11-17. Score: 1

If you just want to affect the artworkFile blocks, you could do this:
{% case "artworkFile" %}
{% for asset in block.downloadUrl %}
    {% if loop.first %}{% endif %}
    
    
    {% if loop.last %}{% endif %}
{% endfor %}

> **Comments:**
> **username_0**: Thanks. This seems to wrap all individual fields rather than all. I've updated my questions with more information.  

---

