For loops in Liquid: using reversed in conjunction with limit:1

Harrison Grieve picture Harrison Grieve · Sep 17, 2012 · Viewed 13.4k times · Source

I'm building a simple blog using Jekyll, and I'm pulling my hair out trying to figure out this problem.

The index page of the site is meant to feature a single, most recent article, with the structure looking something like below (pardon the mess):

{% for post in site.posts reversed limit:1 %}
    <div class="post">
        <div class="post-inner">
            <h3 class="posttitle"><a href="{{ post.url }}">{{ post.title }}</a></h3>
            <p class="postdate">{{ post.date | date: "%d %B %Y" }}</p>
            {{ post.content }}
            <a href="{{ post.url }}#disqus_thread" class="commentLink"></a>
        </div>
    </div>
{% endfor %}

The above template works perfectly fine when the limit is not restrictive (i.e does not exist or is set to the length of the array). It seems to only be when the limit is actually restricting the result that the loop ignores reversed.

I've tried clearing the browser cache, which is what got it working without limit:1, but the progress ends there.

Thanks for the help, and I would be happy to provide more detail if this is not enough.

Answer

Jason Mitchell picture Jason Mitchell · Sep 25, 2012

What is Jekyll actually outputting for you?

From what I understand, the reversed filter is applied last. So let assume you posted the first 15 days of Aug, and lets also say you do something like this:

{% for post in site.posts limit:5 %}
    {{ post.content }}
{% endfor %}

You post array would be ordered in the following pattern

[Aug 15, Aug 14, Aug 13, Aug 12, Aug 11]

And then if you reversed it

{% for post in site.posts reversed limit:5 %}
    {{ post.content }}
{% endfor %}

You post array would be ordered in the following pattern

[Aug 11, Aug 12, Aug 13, Aug 14, Aug 15]

With all that said, I'm a little perplexed as to why you are not using

{% for post in site.posts limit:1 %}