Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

for each key after index "number" create table , key counter Twig

I have a list of sortiments that contain shoe size and pair numbers, taken from a csv file with php and saved as an array in in Twig.

enter image description here

In twig this is my previous code to make a table. the problem here is, it is made to only make tables with 6 columns and not more or less.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<div class="twp-sortiment">
    {% for key in page.extensions.TwpSortiment.data %}
        {% set shoeSizeArray = key.shoesize|split(',') %}
        {% set sortimentArray = key.sortiment|split(',') %}

        {{ sortimentArray[2] }}
            <table>
                <tr>
                    <th>{{ shoeSizeArray[3] }}</th>
                    <th>{{ shoeSizeArray[4] }}</th>
                    <th>{{ shoeSizeArray[5] }}</th>
                    <th>{{ shoeSizeArray[6] }}</th>
                    <th>{{ shoeSizeArray[7] }}</th>
                    <th>{{ shoeSizeArray[8] }}</th>
                </tr>
                <tr>
                    <td>{{ sortimentArray[3] }}</td>
                    <td>{{ sortimentArray[4] }}</td>
                    <td>{{ sortimentArray[5] }}</td>
                    <td>{{ sortimentArray[6] }}</td>
                    <td>{{ sortimentArray[7] }}</td>
                    <td>{{ sortimentArray[8] }}</td>
                </tr>
            </table>
    {% endfor %}
</div>

As you can see i have put my rows in to arrays (shoeSizeArray, shoeSizeArray).
Now i would like to create the table based on how many keys come after index 2, starting from index 3, my table should take values and increase until its at the end. so i tried this but obviously doesnt work as i lack knowladge about Twig.

<div class="twp-sortiment">
    {% for key in page.extensions.TwpSortiment.data %}
        {% set shoeSizeArray = key.shoesize|split(',') %}
        {% set sshoeSizeArray = key.sortiment|split(',') %}
        {{ sortimentArray[2] }}
                    <table>
        <tr>
            {% for key in shoeSizeArray %}
                {% set counter = ( counter | default(2) ) + 1 %}
                <th>{{ shoeSizeArray[counter] }}</th>
            {% endfor %}
        </tr>
        <tr>
            {% for key in sortimentArray %}
                {% set counter = ( counter | default(2) ) + 1 %}
                <td>{{ sortimentArray[counter] }}</td>
            {% endfor %}
        </tr>
    </table>
    {% endfor %}
</div>

This is the output:

enter image description here

As you can see I get 3 more emtpy table columns because im counting every key but I dont want the first 3 keys to be counted in my "for loop",now I have to say count the key of array – 3 (for till index 2: [0],[1],[2]) and then do the and for that amount of

Number of the keyCounter -3.

>Solution :

use slice with split filter https://twig.symfony.com/doc/3.x/filters/slice.html

like

{% set shoeSizeArray = key.shoesize|split(',')|slice(3) %} 
{% set sortimentArray = key.sortiment|split(',')|slice(4) %} 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading