【问题标题】:Incrementing variables in liquid without outputting them增加液体中的变量而不输出它们
【发布时间】:2016-03-20 06:04:57
【问题描述】:

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

但是,当我这样做时

{% increment variable %}

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

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

谢谢

【问题讨论】:

    标签: shopify liquid


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-04
      • 1970-01-01
      • 1970-01-01
      • 2018-06-19
      • 2014-03-30
      • 2011-11-07
      • 2021-12-22
      • 2011-12-16
      相关资源
      最近更新 更多