-
-
Save Chrisedmo/b40a2c8af11069461d3dd71417452298 to your computer and use it in GitHub Desktop.
Craft CMS matrix blocks intelligent wrapping
This file contains hidden or 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
{# _partials/blocks/blocks.html #} | |
{# | |
Blocks component | |
Outputs a matrix field blocks, intelligently wrapping blocks that need to be inset from the edges | |
@param {object} contentBlocks (MatrixBlockModel) | |
#} | |
{# Parameters #} | |
{% set contentBlocks = contentBlocks ?? null %} | |
{# Variables #} | |
{# Define the default bleed state of each known block type #} | |
{% set bleedBlockMap = [] %} | |
{% set bleedBlockMap = bleedBlockMap|merge({ 'textBlock' : false }) %} | |
{% set bleedBlockMap = bleedBlockMap|merge({ 'inlineQuoteBlock': false }) %} | |
{% set bleedBlockMap = bleedBlockMap|merge({ 'buttonBlock' : false }) %} | |
{% set bleedBlockMap = bleedBlockMap|merge({ 'imageBlock' : false }) %} | |
{% set bleedBlockMap = bleedBlockMap|merge({ 'videoBlock' : false }) %} | |
{% set bleedBlockMap = bleedBlockMap|merge({ 'splitPaneBlock' : true }) %} | |
{% set bleedBlockMap = bleedBlockMap|merge({ 'factsBlock' : true }) %} | |
{% set bleedBlockMap = bleedBlockMap|merge({ 'tableBlock' : false }) %} | |
{# wrapper markup #} | |
{% set wrapperOpen = ' | |
<div data-g="wrap wrap--narrow" class="editorial vflow has-theme"> | |
' %} | |
{% set wrapperClose = ' | |
</div> | |
' %} | |
{# Output #} | |
{% if contentBlocks|length %} | |
{% set bleed = [] %} | |
{% for block in contentBlocks %} | |
{# determine the bleed state of the previous block #} | |
{% set bleedPrevious = bleed|last %} | |
{# determine if the block should bleed to the edges of the screen #} | |
{% if block['bleed'] is defined %} | |
{# if it exists, allow a block field 'bleed' to override the default bleed state for this block type #} | |
{% set bleed = bleed|merge([ (block.bleed == '1') ? true : false ]) %} | |
{% else %} | |
{# use the default bleed state for this block type #} | |
{% set bleed = bleed|merge([bleedBlockMap['' ~ block.type ~ '']]) %} | |
{% endif %} | |
{% if bleed|last %} | |
{# yes, block should bleed to edges #} | |
{% if not bleedPrevious and not loop.first %} | |
{# we need to close the wrapper that was opened in the previous loop #} | |
{{ wrapperClose|raw }} | |
{% endif %} | |
{% else %} | |
{# no, block should be wrapped #} | |
{% if bleedPrevious or loop.first %} | |
{{ wrapperOpen|raw }} | |
{% endif %} | |
{% endif %} | |
{# finally, include the block partial #} | |
{% include "_partials/blocks/" ~ block.type %} | |
{% endfor %} | |
{# close wrapper, if open #} | |
{% if not bleed|last %} | |
{{ wrapperClose|raw }} | |
{% endif %} | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment