在帖子的最后一个 Jekyll 循环中添加一个元素

Add an element on the last Jekyll loop of posts

我确定这很简单,但找不到答案。

有一个标准的 Jekyll/Liquid post 迭代器。我如何使用下面的 {% if %} 语句为每个 post 除了最后一个 放置 <hr> 元素?

<ul class="post-list">
    {% for post in site.posts %}
      {% if post.url %}
      <br>
        <li>
          <h2>
            <a class="post-link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
          </h2>
          <span class="post-meta">{{ post.date | date: "%b %-d, %Y" }}</span>
        </li>

        **** {% if post != $last %} ***** How do i do this??
          <hr>
        {% endif %}
      {% endif %}
    {% endfor %}
  </ul>

是的,有一个简单的解决方案。

Liquid 有 forloop object,可以在循环中使用它来访问它的一些属性。
其中一个属性是 forloop.last:

Returns true if it's the last iteration of the for loop. Returns false if it is not the last iteration.

换句话说,你可以这样做:

{% if forloop.last == false %}
  <hr>
{% endif %}