The Wayback Machine - https://web.archive.org/web/20140724085706/http://craftcms.stackexchange.com/questions/658/is-it-possible-to-correctly-output-a-table-within-a-matrix-block
Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

I have set up a Matrix field with three blocks, each contains a Table field.

Each table has one column - a single line text field.

When I call these tables into the template with this code, each row is echoed twice:

{% for block in entry.projectFacts.limit(3) %}
    <div class="panel panel-default">
    {% switch block.type %}
        {% case 'code' %}
            {% if block.codePoints | length %}
                <ul>
                    {% for items in block.codePoints %}
                        /* Outputs "Array" */
                        {{ items }}

                        {% for wtf in items %}
                            /* Outputs identical content & wrapper twice */
                            <li>{{ wtf }}</li>
                        {% endfor %}
                    {% endfor %}
                </ul>
            {% endif %}
    </div>
{% endfor %}

I'd be delighted if someone were able to point out what I need to do differently.

share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

I suppose in your example codePoints is the actual Table field.

In order to output the table rows, the template should look like this:

{% if block.codePoints | length %}
    <ul>
    {% for row in block.codePoints %}
        /* Assuming 'text' is the column handle of the text field */
        <li>{{ row.text }}</li> 
    {% endfor %}
    </ul>
{% endif %}

You get a double output because every row contains an array containing each table field twice, it looks like this:

array(2) { ["col1"]=> string(4) "Some" ["text"]=> string(4) "Some" }

That's why when loop over items you get every text twice.

I hope this helps.

share|improve this answer
    
Thank you Mario - that did it. How did you output that array? –  Petroglyph Creative Jul 1 at 18:48
    
I activated devMode and in my template I used {{ dump(row) }} –  Mario Jul 1 at 19:31
add comment

In your example {{ items }} is the object containing the the table. That's why it outputs "Array".

You are trying to output the table with the loop, which is correct, but it should look like this:

{% for row in items %}
    /* Loop through each table row, and output the column with the handle 'column_handle' */
    <li>{{ row.column_handle }}</li>
{% endfor %}

I assume you have two rows saved in your table, and that's why in your current code, the loop outputs the same content twice.

See also: Table-field templating

.

Edit:

Just saw the double for-loop, see Mario's answer.

share|improve this answer
    
Thanks Victor! I'm actually trying to output the table's content rows - not the handle of the column. Additionally, there are 4 rows in the table, not 2. There are other amounts of rows in the other blocks. Each row is being output correctly, just twice. Any ideas? thanks! –  Petroglyph Creative Jul 1 at 18:19
    
I think you understand the question. Your code definitely works to output the desired content. However, it's still outputting twice instead of once. –  Petroglyph Creative Jul 1 at 18:30
    
I think Mario got it right. –  Victor In Jul 1 at 18:35
add comment

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