gpt4 book ai didi

php - 如何在范围内的液体中进行for循环

转载 作者:可可西里 更新时间:2023-10-31 23:30:30 25 4
gpt4 key购买 nike

我试图在我的模板中使用 for 循环来遍历字母表中的每个字母,如果找到字母表中的第一个字母,则打印出该数据。

我找不到办法做到这一点,我知道如何在 PHP 中做到这一点,但无法让它在液体中工作。

{% for range('a','z') as $i %}   

{% if first_letter == $i %}

{% unless first_letter == current %}

<li class="test"><h2 id='{{ first_letter }}'>{{ first_letter }}</h2> </li>

{% endunless %}

<ul><li class="test"> {{ product_vendor | link_to_vendor }} </li> </ul>

{% assign current = first_letter %}

{% endif %}
{% endfor %}

前两行在这里不起作用,我知道语法不正确,但我不知道如何让它起作用。

最佳答案

liquid for 循环中范围的语法是 here in the Shopify docs :

{% assign num = 4 %}
{% for i in (1..num) %}
{{ i }}
{% endfor %}

{% for i in (3..5) %}
{{ i }}
{% endfor %}

但是,您只能使用范围内的数字,不能使用字符串。

你见过this article吗? ?也许这种方法适用于您的情况。

编辑: Updated link for above article .

关于php - 如何在范围内的液体中进行for循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28581145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com