Last active
April 7, 2017 19:59
-
-
Save samhernandez/380f1a5229f6ab78f4eb871a58bfe657 to your computer and use it in GitHub Desktop.
A macro to iterate through Neo and Matrix blocks
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
{# | |
Don't call this method directly from templates. Rather, call the `neo` or `matrix` | |
methods to keep things future-proof. | |
Block templates may be named (from most generic to most specific) | |
{ block type }_{ entry field handle }_{ entry section handle}_{ entry type}.twig | |
{ block type }_{ entry field handle }_{ entry section handle}.twig | |
{ block type }_{ entry field handle }.twig | |
{ block type }.twig | |
Params: | |
- element: The element (entry, category, asset, etc) that has the matrix field. | |
- handle: The matrix field handle. | |
- params: Additional variables to expose to block templates. Ex. color (optional) | |
- blocks: Optional override to render just these given blocks | |
Output: Renders blocks on a Matrix or Neo field. | |
#} | |
{% macro iterate(rootPath, element, handle, params, blocks) %} | |
{% spaceless %} | |
{% set blocks = blocks | default(element[handle]) %} | |
{% if blocks %} | |
{% set initialParams = params %} | |
{# filter by `enabled` for live preview #} | |
{% for block in blocks if block.enabled %} | |
{# | |
Allow namespaced params. | |
Example: | |
{{ iterate.matrix(entry, 'body', { | |
'color': 'red', | |
'quote.color' : 'pink' | |
}) }} | |
`params.color` will be 'red' everywhere except in the | |
`quote` block where it will be pink | |
#} | |
{% set params = initialParams %} | |
{% for key, value in params %} | |
{% set splitKey = key | split('.') %} | |
{% if splitKey | length and splitKey[0] == block.type %} | |
{% set params = params | merge({(splitKey[1]): value}) %} | |
{% endif %} | |
{% endfor %} | |
{# Set up includes #} | |
{% set includes = [] %} | |
{# If its an entry then add support for section handles #} | |
{% if element.elementType == 'Entry' %} | |
{% set includes = includes | merge([ | |
rootPath ~ block.type ~ '/' ~ handle ~ '_' ~ element.section.handle ~ '_' ~ element.type, | |
rootPath ~ block.type ~ '/' ~ handle ~ '_' ~ element.section.handle | |
]) %} | |
{% endif %} | |
{% set includes = includes | merge([ | |
rootPath ~ block.type ~ '/' ~ handle, | |
rootPath ~ block.type ~ '/index' | |
]) %} | |
{% if craft.config.devMode %} | |
<!-- BEGIN: {{ block.elementType }} | {{ block.type }} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> | |
<!-- Valid template paths: --> | |
{% for item in includes %} | |
<!-- {{ item }}.twig --> | |
{% endfor %} | |
{# Uncomment the line below to list template options on the page #} | |
{# <p>{% for item in includes %}{{ item }}.twig<br>{% endfor %} </p> #} | |
{% endif %} | |
{% if craft.config.devMode %} | |
{% include includes %} | |
{% else %} | |
{% include includes ignore missing %} | |
{% endif %} | |
{% if craft.config.devMode %} | |
<!-- END: {{ block.elementType }} | {{ block.type }} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> | |
{% endif %} | |
{% endfor %} | |
{% endif %} | |
{% endspaceless %} | |
{% endmacro %} | |
{# | |
Iterates through Matrix field blocks | |
#} | |
{% macro matrix(element, handle, params, blocks) %} | |
{% import _self as self %} | |
{{ self.iterate('_/matrix/', element, handle, params, blocks) }} | |
{% endmacro %} | |
{# | |
Iterates through Neo field blocks | |
#} | |
{% macro neo(element, handle, params, blocks) %} | |
{% import _self as self %} | |
{{ self.iterate('_/neo/', element, handle, params, blocks) }} | |
{% endmacro %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment