How to loop navigation using Sorter plugin

70 views
Skip to first unread message

Jared Sampson

unread,
Apr 30, 2013, 12:13:38 PM4/30/13
to hyde-dev
Hi all - 

Great news about the ongoing development of Hyde.  I'm not a particularly great programmer, but would certainly be able to try my hand at writing some documentation, given an example or template to work from.

One quick question. what is the accepted way to make sorted navigation links loop around from back to front (i.e. if I'm looking at the last page, the "next" link points to the first page)?  Ideally, I'm looking for some kind of Boolean setting in the sorter plugin:

    sorter:
        time:
            loop: true

or, less elegantly, something akin to the prev_by_/next_by_ attributes (probably first_by_time/last_by_time) so I could employ my own logic, e.g.:

    {% if resource.prev_by_time %}
        {% set prev = resource.prev_by_time %}
    {% else %}
        {% set prev = resource.last_by_time %}      
    {% endif %}
    <a class="prev" href="{{ content_url(prev.url) }}">
        « {{ prev.meta.title }}
    </a>
    
I had a look at the source and found the declarations for prev_by_/next_by_ but haven't been able to track down any setting for looping around from top to bottom and vice-versa.  I also haven't figured out how to generate first_by_/last_by_ functions, but if anyone wants to take a crack at it, a good place to start would probably be in the last dozen or so lines of hyde/ext/plugins/sorter.py.

Also, if there's another way to do this that I haven't thought of, feel free to set me straight.

Thanks in advance for any help!

Cheers,
Jared

Lakshmi Vyas

unread,
Apr 30, 2013, 12:47:45 PM4/30/13
to hyde...@googlegroups.com
Hi Jared,

The sorter looping seems like a good feature to add. Please add an issue.

In the mean time, you can get the same functionality in the template. For example, here is how you would do it in a the BASIC layout (in blog.j2):

```jinja2
{% set first_by_time = site.content.walk_resources_sorted_by_time()|first %}
{% set last_by_time = site.content.walk_resources_sorted_by_time()|last %}


{% set prev = resource.prev_by_time or last_by_time %}
{% set next = resource.next_by_time or first_by_time %}


<a class="backlink" href="{{ content_url(resource.node.url) }}">Back to list</a>
<a class="prev{{' disabled' if not prev }}"
    title="{{ prev.meta.title }}"
    {% if prev %}
    href="{{ content_url(prev.url) }}"{% endif %}>
    Previous
</a>

<a class="next{{' disabled' if not next }}"
    title="{{ mext.meta.title }}"
    {% if next %}
    href="{{ content_url(next.url) }}"{% endif %}>
    Next
</a>
```


Thanks
Lakshmi

--
You received this message because you are subscribed to the Google Groups "Hyde" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hyde-dev+u...@googlegroups.com.
To post to this group, send email to hyde...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jared Sampson

unread,
May 1, 2013, 12:42:27 PM5/1/13
to hyde-dev
Thanks, Lakshmi.  I'll use the filters (which are so much better than what I was trying to do) and have added an issue to github.  

Cheers,
Jared
Reply all
Reply to author
Forward
0 new messages