我正在尝试设置一个循环来在我的索引页面上显示帖子,以便它们适合 skeleton.css 框架。第一篇文章应该占据十二列,并包含在它自己的行中。之后的每两个帖子都应该单独排成一行。我正在使用以下内容:
{% elsif forloop.index | modulo: 2 == 0 %}
...试图在每两个帖子周围创建一个行 div。这似乎不起作用,因为在输出时,每个单独的帖子都包含在一行 div 中。
<div class="posts">
{% for post in site.posts %}
{% if forloop.first == true %}
<div class="row">
<article class="twelve columns"></article>
</div>
{% elsif forloop.index | modulo:2 == 0 %}
<div class="row">
<article class="six columns"></article>
</div>
{% else %}
<article class="six columns"></article>
{% endif %}
{% endfor %}
</div>
最佳答案
你的 {% elsif forloop.index | modulo:2 == 0 %}
条件不正确,因为在这样的控制结构中不允许使用管道。它最终解析为 {% elsif forloop.index %}
,它始终为真。
你可以这样做:
<div class="posts">
{% for post in site.posts %}
{% assign remainder = forloop.index | modulo: 2 %}
{% if forloop.first == true %}
<div class="row">
<article class="twelve columns"></article>
</div>
{% elsif remainder == 0 %}
<div class="row">
<article class="six columns"></article>
{% if forloop.last %}
</div>
{% endif %}
{% else %}
<article class="six columns"></article>
</div>
{% endif %}
{% endfor %}
</div>
关于ruby - 在 Jekyll Liquid 中对 forloop.index 使用模数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50754073/