gpt4 book ai didi

php - 如何在此液体 for 循环上添加迭代

转载 作者:行者123 更新时间:2023-11-28 00:40:21 25 4
gpt4 key购买 nike

我如何在这个 for 循环上添加一个数字来代替 {{i}}请注意,此循环没有特定限制,因为它会获取所有已订购的商品,无论它们有多少。

<img src="https://billiger.de/sale?shop_id=Acaraa&oid={{ order.order_number }}

{% for line_item in order.line_items %}
&aid_{{i}}={{ line_item.product_id }}&name_{{i}}={{ line_item.product.title }}&cnt_{{i}}={{ line_item.quantity }}&val_{{i}}={{ line_item.product.price | divided_by: 1.19 | times: line_item.quantity | money_without_currency}}
{% endfor %}

" width="1" height="1" border="0" alt="" />

所以循环应该是这样的

&aid_1=ARTICLE-ID-1&name_1=ARTICLE-NAME-1&cnt_1=ARTICLE-COUNT-1&val_1=ARTICLE-VALUE-1
&aid_2=ARTICLE-ID-2&name_2=ARTICLE-NAME-2&cnt_2=ARTICLE-COUNT-2&val_2=ARTICLE-VALUE-2
&aid_3=ARTICLE-ID-3&name_3=ARTICLE-NAME-3&cnt_3=ARTICLE-COUNT-3&val_3=ARTICLE-VALUE-3

PS:shopify模板文件中会用到以上代码

谢谢

最佳答案

forloop 对象有许多辅助方法,包括计算循环当前迭代次数的方法:

  • forloop.index 从1开始统计循环次数
  • forloop.index0 从0开始计算循环次数

在您的示例中,您将修改代码以使用适当的方法而不是 {{i}};即:

<img src="https://billiger.de/sale?shop_id=Acaraa&oid={{ order.order_number }}

{% for line_item in order.line_items %}
&aid_{{forloop.index}}={{ line_item.product_id }}&name_{{forloop.index}}={{ line_item.product.title }}&cnt_{{forloop.index}}={{ line_item.quantity }}&val_{{forloop.index}}={{ line_item.product.price | divided_by: 1.19 | times: line_item.quantity | money_without_currency}}
{% endfor %}

" width="1" height="1" border="0" alt="" />

我假设您想在这里从 1 开始计算迭代次数,但如果您想从 0 开始,只需使用 forloop.index0 >.

Here is the documentation for forloop.index用于 Shopify 的官方 Liquid 库(在 Ruby 中)。

由于您已将问题标记为 PHP,因此看起来您正在使用 php-liquid库的端口 - 它出现在 Shopify 的 Liquid ports 列表中.

这个端口似乎没有任何详细的文档,但是如果你看一下 unit tests您会看到这些 forloop 帮助器已被实现。

希望这有帮助:)

关于php - 如何在此液体 for 循环上添加迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53836766/

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