Last active
August 11, 2021 01:03
-
-
Save danieltott/a0959e6d2108a3a15285ea2f184530c0 to your computer and use it in GitHub Desktop.
Matrix in Matrix Workaround
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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! #} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
`
{% macro outputThumbs(thumbList) %}
{# use macro to keep it DRY #}
{% if entry.testMatrix is defined %}
{% set thumbList = [] %}
{% for block in entry.customStory %}
{% endfor %}
{% endif %}`