是否有“休息”?标签来逃避 Liquid 中的循环?

发布于 2024-12-14 05:49:00 字数 355 浏览 3 评论 0原文

如何跳出 Liquid 中的循环(主要是 for 循环)?我尝试过 {% break %},但失败并显示保存文件时出错:未知标记“break”

我正在努力实现类似的目标:

var variants = [];
{% for item in cart.items %}
    {% if item.product.handle == "handle-name" %}
    variants = {{item.product.variants | json}};
    {% break %} // won't work
    {% endif %}
{% endfor %}

How do I break out of loop in Liquid, mainly a for-loop? I've tried {% break %}, but that fails with There were errors saving your file: Unknown tag 'break'.

I'm trying to achieve something like:

var variants = [];
{% for item in cart.items %}
    {% if item.product.handle == "handle-name" %}
    variants = {{item.product.variants | json}};
    {% break %} // won't work
    {% endif %}
{% endfor %}

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

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

发布评论

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

评论(1

寄居人 2024-12-21 05:49:00

对于未来的访客。上面的代码确实在当前的 Liquid (gem v2.5.1) 中工作。

因此,您可以简单地执行以下操作:

{% for item in cart.items %}
    {% if item.product.handle == "handle-name" %}
    variants = {{item.product.variants | json}};
       {% break %} // This will work
    {% endif %}
{% endfor %}

For future visitors. Above code does work in current Liquid (gem v2.5.1).

So, you can simply do:

{% for item in cart.items %}
    {% if item.product.handle == "handle-name" %}
    variants = {{item.product.variants | json}};
       {% break %} // This will work
    {% endif %}
{% endfor %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文