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

标签 ruby for-loop jekyll liquid

我正在尝试设置一个循环来在我的索引页面上显示帖子,以便它们适合 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/

相关文章:

javascript - 如何组合多个类似ctags的标签工具?

markdown - 如何在 Jekyll 中从 1 以外的数字开始有序列表?

ruby 使用 strptime 和 strftime 解析日期

ruby - 正则表达式以匹配具有可选条件的字符串

linux - 使用 linux 命令行识别文件中具有 2 个字段的重复行

r - 有没有办法使用 dplyr 根据除以另一列的 group_by 来创建新列?

Front Matter 中的 Jekyll if 语句

jekyll - 为什么删除 yaml front matter 会阻止 Jekyll 将 md 文件转换为 html

ruby-on-rails - Gem::Ext::BuildError: 错误:无法构建 gem native 扩展。在 VScode 中运行 bundler install 时不断收到此消息

r - 嵌入在字符串名称中的 R 变量中的 for 循环