我正在努力想出如何在Liquid/Jekyll的for循环中递增索引变量。目前,我的代码大致如下:
{% for i in (0..num_posts) %}
{% if i < some_value %}
do_thing
{% else %}
{% endif %}
{% assign i = i|plus:1 %}
{% if i<some_value %}
do_another_thing
{% else %}
{% endif %}
{% endfor %}
问题是,它没有将i递增,而是保留了i的原始值。
我尝试过的方法:
- 使用
{% assign i = i|plus:1 %}
。 - 使用
{% increment i %}
。 使用
{% assign j = i|plus:1 %} {% assign i = j %}
我也不能使用 offset
命令,因为代码在循环中不总是仅检查 2 个 if 语句。
有什么想法吗?