在不输出变量的情况下增加液体中的变量

Incrementing variables in liquid without outputting them

我在 shopify 中做一个 for 循环,我需要增加一个变量。

然而,当我这样做时

{% increment variable %}

除了增加它,它还会在屏幕上显示输出!

我简直不敢相信。有没有办法避免这种情况?

谢谢

这是设计使然,它允许您同时递增和显示变量。参见 the documentation

assign 只允许您分配 new 变量(并且不能修改现有变量),因此除了创建新标签之外,最简单的方法是使用 use capture 捕获输出:

{% capture _ %}{% increment variable %}{% endcapture %}

话虽这么说,也许是时候重新考虑一下您为什么要这样做了?请注意,您已经有 forloop.indexforloop.index0 可用于循环索引(再一次,see the documentation)。

如果您使用与 forloop.index 不同的逻辑来增加值,您可以使用 plus filter 来增加变量:

{% assign variable = 0 %}
{% for … %}
  {% assign variable = variable | plus: 1 %}
{% endfor %}

我还可以建议您看一下 cheat sheet for Shopify