views:

40

answers:

3

I'm trying to use the jinja2 templating langauge to return the last n(say, 5) posts in my posts list:

{% for recent in site.posts|reverse|slice(5) %}
    {% for post in recent %}
        <li> <a href="/{{ post.url }}">{{ post.title }}</a></li>
    {% endfor %}
{% endfor %}

This is returning the whole list though. How do you strip the first or last n elements?

A: 

Looking at Jinja2 template, I think the following is sufficient.

site.posts is already a chronological list of all posts.

{% for post in site.posts|reverse|slice(5) %}
     <li> <a href="/{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
pyfunc
That's sensible, but then jinja2 outputs 5 empty li's....I'm not sure what the extra wrapper layer of post in recent is for.
Jesse Spielman