18

I'm sure this is simple but cant find the answer.

There is a standard Jekyll/Liquid post iterator. How do i use the {% if %} statement below to put the <hr> element for each post except the last?

<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>
David
  • 3,166
  • 2
  • 30
  • 51

1 Answers1

32

Yep, there's an easy solution for this.

Liquid has the forloop object, which can be used inside a loop to access some of its properties.
One of those properties is forloop.last:

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

In other words, you can just do this:

{% if forloop.last == false %}
  <hr>
{% endif %}
Christian Specht
  • 35,843
  • 15
  • 128
  • 182