如果我在Liquid中有一个for循环(使用Jekyll),我怎么能只针对偶数(或奇数)项?我试过了:
{% for item in site.posts %}
{% if forloop.index % 2 == 1 %}
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用.我也尝试过:
(forloop.index % 2) == 1
Run Code Online (Sandbox Code Playgroud)
和:
forloop.index - (forloop.index / 2 * 2) == 1
Run Code Online (Sandbox Code Playgroud)
Ale*_*nde 69
我想你会想要使用循环标签.例如:
{% for post in site.categories.articles %}
<article class="{% cycle 'odd', 'even' %}"></article>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
如果您希望每个周期使用不同的HTML标记:
{% for item in site.posts %}
{% capture thecycle %}{% cycle 'odd', 'even' %}{% endcapture %}
{% if thecycle == 'odd' %}
<div>echo something</div>
{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
你可以在Liquid for Designers找到更多关于它的信息,尽管那里的例子没有特别的帮助.这个Shopify支持线程也应该有所帮助.
Chr*_*cht 24
与此相反,以什么Shopify支持线程在强麦揽得的回答说,有是一个modulo在液相功能-在形式的modulo过滤器.
有了它,你可以这样做:
{% for item in site.posts %}
{% assign mod = forloop.index | modulo: 2 %}
{% if mod == 0 %}
<!-- even -->
{% else %}
<!-- odd -->
{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)