0

So I have a matrix field with 3 different block types. The first two block types have markup contained within their scope. The last type, has opening and closing tags for the first of type and last of type (basically creating a row with two columns). I was able to successfully get the markup, but wanted to see if there is a better way to to this write this.

{% for block in entry.homeContentBlocks %}
    {% if block.type == "leadGeneration" %}
        ...
    {% elseif block.type == "banner" %}
        ...
    {% else %}
        {% if block.next() %}
            {% if block.next().type == "contentBlock" %}<div class="row">{% endif %}
        {% endif %}

            {% if block.next() %}
                {% if block.next().type == "contentBlock" %}
                    <div class="block-6 v-divider">
                {% endif %}
            {% endif %}
            {% if block.prev() %}
                {% if block.prev().type == "contentBlock" %}
                    <div class="block-6 pad-left">
                {% endif %}
            {% endif %}

                <h3>{{ block.heading }}</h3>
                {{ block.body }}
                {% if block.btnLink | length %}
                    {% set link = block.btnLink.first().url %}
                    <a href="{{ link }}" class="btn">{{ block.btnLabel }}</a>
                {% endif %}
            </div>

        {% if block.prev() %}
            {% if block.prev().type == "contentBlock" %}</div>{% endif %}
        {% endif %}
    {% endif %}
{% endfor %}

So this works, but let's say the blocks out of sequence. I thought maybe a increment variable could work, but wasn't sure how to implement it.

  • I wonder if I should've setup the block type to have both columns info contained within one block on the admin side. This would simplify the template logic and most likely minimize data entry error. Hmmmm... – Siebird Sep 23 '14 at 20:43
1

Why don't you do something like this? You can use switch and case to change the output of each part of the matrix (we call them blocks).

This is part of a snippet to output a documentation matrix that contains code, titles, content etc.

{% for block in entry.documentation %}
    {% switch block.type %}

         {% case 'subsectionTitle' %}
            <h2 id="{{ anchor }}">{{ block.subsectionTitle }} <a href="#{{ anchor }}" class="pound">#</a></h2>

        {% case 'description' %}
             <div class="docs-description">{{ block.description }}</div>

    {% endswitch %}
{% endfor %}
  • This won't solve the problem of the opening and closing tags of the parent div row. So the first type opens the <div class="row"> and the last of type closes the parent row </div>. I may change the block so everything is contained within the scope (see my comment below the post) – Siebird Sep 23 '14 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.