Limit a list of posts to N entries (sorter extension, jinja2)

1,831 views
Skip to first unread message

fvsch

unread,
Mar 14, 2011, 10:37:45 AM3/14/11
to Hyde
In one of the example templates I looked at, I found this code listing
a series of pages by publication time:

{% for res in resource.node.walk_resources_sorted_by_time() %}

{% endfor %}

After a bit of trial and error and looking at sorter.py, I found that
the "time" keyword in walk_resources_sorted_by_time must match a
sorter.time object in the site's YAML config. I tweaked mine and got:

sorter:
time:
attr: meta.datetime
reverse: false
filters:
source.kind: html
meta.is_article: true
meta.status: published

I don't use reverse:true because I use this sorter for displaying next/
previous posts. With reverse:true, the next post was the nearest older
post, and the previous post was the nearest fresher post, and it was
counter-intuitive.

Then, on my home page I want to list the 3 latest posts. The only code
I tried that managed this is:

{% for post in resource.node.walk_resources_sorted_by_time()|reverse()
%}
{% if loop.index < 4 %}

{% endif %}
{% endfor %}

My question: can I limit the number of posts to list, in the YAML
config or in templates? I tried a few things in the Jinja2
documentation but it was not very clear on this and I got a few Python
exceptions for different solutions I tried.

Lakshmi Vyas

unread,
Mar 14, 2011, 11:40:16 AM3/14/11
to hyde...@googlegroups.com
Hi,

> My question: can I limit the number of posts to list, in the YAML
> config or in templates?


Hyde enables jinja2 loop controls (http://jinja.pocoo.org/docs/extensions/#loopcontrols-extension).

So you can break out of the loop when index is 4.

{% if loop.index > 3 %} {% break %} {% endif %}

=====================================================================================
OT:

Not sure if you have already found it - but with sorters (also with groupers and taggers) you have:


prev_by_time, next_by_time (sorter)

prev_in_group, next_in_group (grouper)

prev_with_tag, next_with_tag (tagger)

=====================================================================================


Thanks
Lakshmi.

> --
> You received this message because you are subscribed to the Google Groups "Hyde" group.
> To post to this group, send email to hyde...@googlegroups.com.
> To unsubscribe from this group, send email to hyde-dev+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/hyde-dev?hl=en.
>

fvsch

unread,
Mar 14, 2011, 12:09:02 PM3/14/11
to Hyde
Nice. I switched to using loop controls.

For prev_by_time/next_by_time, I’m already using them (found them in
the code for cloudpanic if I remember correctly). For instance, this
HTML code:
<p><strong>Previously</strong>
<a href="/2011/labor/" lang="en">Labor On</a></p>
<p><strong>Next post</strong>
<a href="/2011/pagedate/" lang="en">Article Date in Google Results</
a></p>
…is generated with:
{% if resource.prev_by_time %}
{% set p = resource.prev_by_time %}
<p><strong>Previously</strong>
<a href="{{ content_url(p.url) }}" lang="{{ p.meta.title_lang|
default(p.meta.language) }}">{{ p.meta.title }}</a></p>
{% endif %}
{% if resource.next_by_time %}
{% set p = resource.next_by_time %}
<p><strong>Next post</strong>
<a href="{{ content_url(p.url) }}" lang="{{ p.meta.title_lang|
default(p.meta.language) }}">{{ p.meta.title }}</a></p>
{% endif %}

Lakshmi Vyas

unread,
Mar 15, 2011, 9:07:08 AM3/15/11
to hyde...@googlegroups.com
Hi,

I have added two filters: top and islice doing some of these list manipulations.

See examples here:
https://github.com/hyde/hyde/blob/master/hyde/tests/test_jinja2template.py#L571-578

Thanks
Lakshmi

Reply all
Reply to author
Forward
0 new messages