Skip to content

Instantly share code, notes, and snippets.

@seb-thomas
Last active January 12, 2016 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seb-thomas/e7e839a7f5df847fdb89 to your computer and use it in GitHub Desktop.
Save seb-thomas/e7e839a7f5df847fdb89 to your computer and use it in GitHub Desktop.
Craft CMS template code for wrapping every 2 half width blocks in a row
{# Set outside of block iterator #}
{% set number_of_halves = 0 %}
{% for block in entry.casestudyModules %}
{# Full width block types #}
{% if block.type == "slider" %}
{% if block.slideImage | length %}
{% include 'modules/slider-module' %}
{% endif %}
{% elseif block.type == "textBlock" and block.width == "full" %}
{% include 'modules/casestudy-copy-module' %}
{% elseif block.type == "image" and block.width == "full" %}
{% set image = block.image.first() %}
{% if image %}
<div class="module casestudy-hero-image">
<img class="img-responsive" src="{{ image.getUrl() }}" alt="">
</div>
{% endif %}
{# Half width block types #}
{% elseif block.type == "textBlock" or block.type == "image" and block.width == "half" %}
{# Increment number_of_halves here so only half block are counted, not all blocks #}
{% set number_of_halves = number_of_halves + 1 %}
{# Blocks are in an array. We can think of it as ['full', 'half', 'half', 'half', 'half', 'full', 'half', 'half' ... ] #}
{# Current block will be a 'half'. Conditions here confirm its pre/succeeding block.widths, and thus whether it should be wrapped #}
{# if there is a previous block to check #}
{% if block.prev %}
{# It must be a half width block, but is it a 1st or 2nd half? Counter tells us the position #}
{% if number_of_halves is odd %}
<div class="row row-of-halves">
{% endif %}
{# if there isn't a previous block start a row #}
{% else %}
<div class="row row-of-halves">
{% endif %}
{# The actual block content #}
<div class="col-md-6 half-block">
{% if block.type == "textBlock" %}
...
{% else %}
...
{% endif %}
</div>
{# if there is a next block to check #}
{% if block.next %}
{% if number_of_halves is even %}
</div>
{% endif %}
{# if there isn't another block end a row #}
{% else %}
</div>
{% endif %}
{% endif %}
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment