Skip to content

Instantly share code, notes, and snippets.

@danieltott
Last active August 11, 2021 01:03
Show Gist options
  • Save danieltott/a0959e6d2108a3a15285ea2f184530c0 to your computer and use it in GitHub Desktop.
Save danieltott/a0959e6d2108a3a15285ea2f184530c0 to your computer and use it in GitHub Desktop.
Matrix in Matrix Workaround
{% macro outputTiles(tileList) %}
{# use macro to keep it DRY #}
<div class="tiles">
{% for tile in tileList %}
<div class="tile">{{ tile.tileTitle }}</div>
{% endfor %}
</div>
{% endmacro %}
{% if entry.testMatrix is defined %}
{# start with empty array #}
{% set tileList = [] %}
{# loop through matrix blocks like normal #}
{% for block in entry.testMatrix %}
{% if block.type != "tile" and tileList|length %}
{# here's where we actually output the tile group #}
{{ _self.outputTiles(tileList) }}
{# once we've outputted the tiles, empty the list #}
{% set tileList = [] %}
{% endif %}
{# normal switch on block.type #}
{% switch block.type %}
{% case "textBlock" %}
<div class="textBlock">Text Block</div>
{% case "imageBlock" %}
<div class="imageBlock">Image Block</div>
{% case "callToAction" %}
<div class="callToAction">Call to Action</div>
{% case "tile" %}
{# instead of outputting anything, we save this tile to our list for later outputting #}
{% set tileList = tileList|merge([block]) %}
{% endswitch %}
{% endfor %}
{# once we've finished the loop, we output any tiles that were at the end of the loop #}
{% if tileList|length %}
{{ _self.outputTiles(tileList) }}
{% set tileList = [] %}
{% endif %}
{% endif %}
{# that's it! #}
@Rory-C
Copy link

Rory-C commented Sep 14, 2017

Hi Daniel,

This workaround is perfect for a project I'm working on, but I'm having difficulty getting the macro to find where my content actually is. Basically, what's appearing onscreen is outputting the html but can't seem to find the content from the matrixblocks (they just appear blank). I've attached a screenshot of my matrixblock set-up.
I just can't quite seem to get my head around how to correctly find and output the the image and text I'm looking for, for each tile/thumbnail.

Any help is greatly appreciated

simulated_matrixwithinmatrix

`
{% macro outputThumbs(thumbList) %}
{# use macro to keep it DRY #}

<!-- Grid - Small Thumb -->
{% for thumb in thumbList %}
{{ block.thumbnailText }}
{% endfor %}
{% endmacro %}

{% if entry.testMatrix is defined %}
{% set thumbList = [] %}

{% for block in entry.customStory %}

{% if block.type != "thumbnailGridSmall" and thumbList|length %}

{% set image = block.thumbnailImage.first() %}
        {{ _self.outputThumbs(thumbList) }}
        {% set thumbList = [] %}
        

{% elseif block.type == "mainHeader" %}


<div class="row custom-full">
    <div class="custom-header-main custom-center">
        {{ block.header }}
    </div>
    {% if block.headDivider %}
    <div class="c-head-divider"></div>
    {% endif %}
</div>


{% elseif block.type == "thumbnailGridSmall" %}

            {% set thumbList = thumbList|merge([block]) %}

{% endif %}

{% endfor %}

{% if thumbList|length %}
    {{ _self.outputThumbs(thumbList) }}
    {% set thumbList = [] %}
{% endif %}

{% endif %}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment