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

Time:03-02

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.

<div >
    {% 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 >
    {% 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>

i know i hurt ur eyes with my bad code but i just kept it to let u know what path i want to go.
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.
Any help or feedback is appreciated <3. Best Regards Zy

CodePudding user response:

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) %} 
  • Related