Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add an element on the last Jekyll loop of posts

Tags:

jekyll

liquid

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>
like image 525
David Avatar asked Jan 02 '16 16:01

David


1 Answers

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 %}
like image 61
Christian Specht Avatar answered Nov 09 '22 10:11

Christian Specht