对于索引“number”之后的每个键创建表,密钥计数器 Twig

发布于 2025-01-11 02:41:54 字数 2763 浏览 2 评论 0原文

我有一个包含鞋号和鞋号的分类列表,这些列表是用 php 从 csv 文件中获取的,并在 Twig 中保存为数组。

输入图片此处描述

在 twig 中,这是我之前制作表格的代码。这里的问题是,它只制作有 6 列的表格,而不是更多或更少。

<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>

正如您所看到的,我已将行放入数组中(shoeSizeArray、shoeSizeArray)。
现在我想根据索引 2 之后有多少个键来创建表,从索引 3 开始,我的表应该取值并增加直到最后。所以我尝试了这个,但显然不起作用,因为我缺乏关于 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>

这是输出:

在此处输入图像描述

如您所见,我得到了另外 3 个空表列,因为我正在计算每个键,但我不希望在我的“for 循环”中计算前 3 个键,现在我有说算数组的键 - 3 (直到索引 2:[0],[1],[2]),然后对

keyCounter -3 的数量执行 and 操作。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦慕雪 2025-01-18 02:41:54

使用带有分割过滤器的切片 https://twig.symfony.com/doc /3.x/filters/slice.html

喜欢

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

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) %} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文