液体模板:for循环中的偶/奇项目 [英] Liquid templates: even/odd items in for loop
本文介绍了液体模板:for循环中的偶/奇项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我在Liquid(使用Jekyll)中有一个for循环,我该如何仅定位偶数(或奇数)项?我已经尝试过:
If I have a for loop in Liquid (using Jekyll), how can I target even (or odd) items only? I have tried:
{% for item in site.posts %}
{% if forloop.index % 2 == 1 %}
但是似乎不起作用.我也尝试过:
but that doesn't seem to work. I have also tried:
(forloop.index % 2) == 1
和:
forloop.index - (forloop.index / 2 * 2) == 1
推荐答案
我想您将为此使用循环标记.例如:
I think you'll want to use the cycle tag for this. For example:
{% for post in site.categories.articles %}
<article class="{% cycle 'odd', 'even' %}"></article>
{% endfor %}
如果您希望每个周期使用不同的HTML标记:
If you want different HTML markup for each cycle:
{% for item in site.posts %}
{% capture thecycle %}{% cycle 'odd', 'even' %}{% endcapture %}
{% if thecycle == 'odd' %}
<div>echo something</div>
{% endif %}
{% endfor %}
您可以在设计师液体中找到有关其的更多信息,尽管该示例并没有特别的帮助.此 Shopify支持线程也应有所帮助.
You can find more information on it at Liquid for Designers, although the example there isn't particularly helpful. This Shopify support thread should also help.
这篇关于液体模板:for循环中的偶/奇项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文