在 Jekyll Liquid 中对 forloop.index 使用模数

Nat*_*rch 5 ruby for-loop liquid jekyll

我正在尝试设置一个循环以在我的索引页面上显示帖子,以便它们适合 skeleton.css 框架。第一个帖子应该占据十二列,并包含在它自己的行中。此后每两个帖子应单独包装在一行中。我正在使用以下内容:

{% elsif forloop.index | modulo: 2 == 0 %}
Run Code Online (Sandbox Code Playgroud)

...试图在每两个帖子周围创建一个行 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>
Run Code Online (Sandbox Code Playgroud)

Dav*_*uel 8

您的{% 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>
Run Code Online (Sandbox Code Playgroud)