1

I have a matrix table field that I want to render on a page. I've tried a few different things and nothing is working. I tried switch and elseif, but those weren't working. I'm no longer getting errors, but now - even though I have content in the fields - nothing shows up on the page.

It seems like it must be a templating error but I can't see what it could be. Any help would be greatly appreciated.

{% for block in entry.comparisonChart %} 
{% if block.type == 'compareChartHeadingBlock' %}
  <h3>{{ block.compareChartHeading }}</h3>  
{% endif %}

{% if block.type == 'compareChartTwoBlock' %}
    {% for row in block.compareChartTwo %}  
    <div class="row">
      <div class="box1-3 {{ row.empty }}">{{ row.chartTwoLeft }}</div>
      <div class="{{ row.chartTwoAgree }} {{ row.chartTwoDisagree }} {{ row.chartTwoSeparate }}"></div>
      <div class="box1-3 {{ row.empty }}">{{ row.chartTwoRight }}</div>
    </div>
     {% endfor %}
  {% endif %}


  {% if block.type == 'compareChartThreeBlock' %}
    {% for row in block.compareChartThree %}  
    <div class="row">
      <div class="box1-5 {{ row.empty }}">{{ row.chart3Left }}</div>
      <div class="{{ row.chart3LeftAgree }} {{ row.chart3LeftDisagree }} {{ row.chart3LeftSeparate }}"></div>
      <div class="box1-5 {{ row.empty }}">{{ row.chart3Center }}</div>
      <div class="{{ row.chart3RightAgree }} {{ row.chart3RightDisagree }} {{ row.chart3RightSeparate }}"></div>
      <div class="box1-5 {{ row.empty }}">{{ row.chart3Right }}l</div>
    </div>
   {% endfor %}
  {% endif %}     
  {% endfor %}

Here is the matrix field:

enter image description here

1

First, make sure you have devMode enabled so any errors will be show. Then, I would consider writing the template code in a way that will ensure something will output when looping through the block types to help troubleshoot.

For example

{# Loop through each block #}
{% for block in entry.comparisonChart %}
    {# Output templating based on the block type #}
    {% switch block.type %}
        {% case "compareChartHeadingBlock" %}
            <h3>{{ block.compareChartHeading }}</h3>  

        {% case "compareChartTwoBlock" %}
            {% for row in block.compareChartTwo %}  
                <div class="row">
                  <div class="box1-3 {{ row.empty }}">{{ row.chartTwoLeft }}</div>
                  <div class="{{ row.chartTwoAgree }} {{ row.chartTwoDisagree }} {{ row.chartTwoSeparate }}"></div>
                  <div class="box1-3 {{ row.empty }}">{{ row.chartTwoRight }}</div>
                </div>
            {% else %}
                Compare chart two has no content
            {% endfor %}

        {% case 'compareChartThreeBlock' %}
            {% for row in block.compareChartThree %}  
                <div class="row">
                    <div class="box1-5 {{ row.empty }}">
                        {{ row.chart3Left }}
                    </div>
                    <div class="{{ row.chart3LeftAgree }} {{ row.chart3LeftDisagree }} {{ row.chart3LeftSeparate }}"></div>
                    <div class="box1-5 {{ row.empty }}">{{ row.chart3Center }}</div>
                    <div class="{{ row.chart3RightAgree }} {{ row.chart3RightDisagree }} {{ row.chart3RightSeparate }}"></div>
                    <div class="box1-5 {{ row.empty }}">{{ row.chart3Right }}l</div>
                </div>
            {% else %}
                Compare chart three has no content
            {% endfor %}

        {% default %}
            {# The message below will be displayed if the type doesn’t match those handled above #}
            This {{ block.type }}’s templating has not been handled

    {% endswitch %}

{# If the Matrix has no blocks, output a message #}
{% else %}
    This Matrix has no content
{% endfor %}

An approach like the above should help narrow where the problem is occuring. If you do get error messages, please post them as it can help identify where the problem is occuring as well.

| improve this answer | |
  • Thank you Stephen! That helped out so much :) I was able to diagnose what was happening (mismatched labeling of matrix blocks). Appreciate the help! Working great now – Jay247 Mar 26 '18 at 13:42

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.