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="https://stackoverflow.com/{{ 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?

I had the same problem too. It’s a simple answer. This retrieves the last five items in site.posts:

{% for recent in site.posts[-5:] %}
    {% for post in recent %}
        <li> <a href="https://stackoverflow.com/{{ post.url }}">{{ post.title }}</a></li>
    {% endfor %}
{% endfor %}

this is a bit simpler I think without the use of the slice filter:

{% for post in site.posts | reverse | list[0:4] %}
  <li>&raquo; <a href="https://stackoverflow.com/{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}

another way is to use the loop controls extension:

{% for post in site.posts | reverse %}
  {%- if loop.index > 4 %}{% break %}{% endif %}
  <li>&raquo; <a href="https://stackoverflow.com/{{ post.url }}">{{ post.title }}</a></li>
{%- endfor %}

I came up with the following code:

{% for x in xs | batch(n) | first %}
    ...
{% endfor %}

The batch(n) filter splits a list xs into sublists of length n, then the first filter selects the first of these sublists.

Try subscript notation, as in normal Python. For example, to take the last 5 posts and display them in reverse order:

import jinja2
tmpl = """\
{%- for col in posts[-5:]|reverse|slice(3) -%}
    {%- for post in col -%}
        {{ post }}
    {%- endfor -%}
    <br>
{%- endfor -%}"""
jinja2.Template(tmpl).render(posts=[1,2,3,4,5,6,7])

produces: u'76<br>54<br>3<br>'

For me, the following simple code works and doesn’t require the whole chain of jinja filters. Simply use the list filter to convert to list and then do normal array slicing (note the parantheses):

{% for recent in (site.posts | list)[-5:] %}
  {% for post in recent %}
    <li> <a href="https://stackoverflow.com/{{ post.url }}">{{ post.title }}</a></li>
  {% endfor %}
{% endfor %}

I had the same problem, but my data was in a sequence rather than a list and this code handles both.

@Andrey’s answer has the right idea. However, to fully solve your question:

{% for recent in site.posts|batch(5)|list|last|reverse %}
        <li> <a href="https://stackoverflow.com/{{ recent.url }}">{{ recent.title }}</a></li>
{% endfor %}

Alternatively:

{% for recent in site.posts|reverse|batch(5)|first %}
        <li> <a href="https://stackoverflow.com/{{ recent.url }}">{{ recent.title }}</a></li>
{% endfor %}

Whichever one you use depends on your preferences.

{% for recent in site.posts[-5:][::-1] %}
    {% for post in recent %}
        <li> <a href="/{{ post['url'] }}">{{ post['title'] }}</a></li>
    {% endfor %}
{% endfor %}
  1. [-5:] – last 5 posts
  2. [::-1] – reverse order

To get the last element, get total index from the array list.

For example, your object name is foundappointmentlog.

{% set total=foundappointmentlog|length %} //it return length
{{foundappointmentlog[total-1].appointment_result}}  // here you get your last value using index