1

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 %}
 <a href="{{ asset.getUrl() }}" target="_blank">
 <div class="file {{ block.platform }} gutter-bottom--half"></div></a>
{% endfor %}

Desired Output

<div class="wrapper">
  <a href="asseturl1.png>
   <div class="file exampleplatform1 gutter-bottom--half"></div>
  </a>
  <a href="asseturl2.png>
   <div class="file exampleplatform2 gutter-bottom--half"></div>
  </a>
</div>

Matrix Setup enter image description here

2
  • 2
    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>? Nov 16, 2015 at 11:56
  • 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
    Nov 20, 2015 at 16:23

2 Answers 2

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.:

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

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 <div class="wrapper"> container, e.g.:

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

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 %}
                <a href="{{ asset.getUrl() }}" target="_blank">
                    <div class="file {{ block.platform }} gutter-bottom--half"></div>
                </a>
            {% 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 #}
<div class="wrapper">
    {{ artworkFileBlocks|join('')|raw }}
</div>
2
  • 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
    Nov 22, 2015 at 10:30
  • 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. Nov 22, 2015 at 12:07
1

If you just want to affect the artworkFile blocks, you could do this:

{% case "artworkFile" %}
{% for asset in block.downloadUrl %}
    {% if loop.first %}<div class="wrapper">{% endif %}
    <a href="{{ asset.getUrl() }}" target="_blank">
    <div class="file {{ block.platform }} gutter-bottom--half"></div></a>
    {% if loop.last %}</div>{% endif %}
{% endfor %}
1
  • Thanks. This seems to wrap all individual fields rather than all. I've updated my questions with more information.
    – sarah3585
    Nov 20, 2015 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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