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>
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. Returnsfalse
if it is not the last iteration.
In other words, you can just do this:
{% if forloop.last == false %}
<hr>
{% endif %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With