Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to link to previous / next page? #1474

Closed
steinsag opened this issue Sep 22, 2014 · 7 comments
Closed

How to link to previous / next page? #1474

steinsag opened this issue Sep 22, 2014 · 7 comments

Comments

@steinsag
Copy link

I tested the 3.5dev version with the #420 feature and it works perfectly! Pages / articles are now sorted by my custom attribute.

Now I tried to output a previous / next button below my page by altering my page.html theme file. However, Jinja2 templates are really limited as I can't have local variables. How would you solve this problem so that I can output the next or previous page? Please note, I separate my pages into different categories (i.e. chapters), so I'm only interested in the previous / next page of the same category. Any pointers?

@ingwinlu
Copy link
Contributor

iterate over pages, check if page order is +1 or -1 to your current pages sort_order, also, please come and join us in IRC for such questions :)

@steinsag
Copy link
Author

@ingwinlu It's not so easy, because I don't use consecutive numbers for my sorting. I introduce gaps (e.g. 10, 20, 30) so that I can easily resort my pages without having to change sorting of all pages.

I tried the following loop:

{% set previous = page %}
{% for pg in pages %}
    {% if pg.category == page.category %}
        {% if pg.weight < page.weight %}
            {% set previous = pg %}
        {% endif %}
    {% endif %}
{% endfor %}

previous: {{previous.title}}

However, this always just outputs the current page, because I can't set previous to a value in the inner loop. It seems Jinja variables can only be set once, but not change their value.

@ingwinlu
Copy link
Contributor

you could sort pages via the weight attribute, then go ahead and loop on everything that is above/below your current one and hit the first one that has the correct category you want

@ingwinlu
Copy link
Contributor

something like this, you probably need to play around with it a bit:

{% for pg in pages|selectattr("category",page.category)|sort(attribute='weight') %}

if that works you still need to determine the index of the current page and select the next/prev one

@steinsag
Copy link
Author

Thanks for looking into it, but if there is no way to call real python code, I don't think it is possible. To find the previous post, I need to iterate over the sorted list of pages and stop when I find the current one. In the loop, I need a variable, to keep track of the previous page. This would require defining the variable outside the loop and just setting the value inside the loop, which is not possible in jinja (unless you show me how to do it).

Is there a way to include python code like a utility class in a theme?

@avaris
Copy link
Member

avaris commented Sep 23, 2014

@steinsag you can use JINJA_FILTERS to add custom python functions to be available in your themes. So in your settings:

def prev_page(current, pages):
    prev_page = None
    for page in pages:
        if page.category == current.category and page.weight < current.weight:
            prev_page = page
    return prev_page

def next_page(current, pages):
    next_page = None
    for page in pages:
        if page.category == current.category and page.weight > current.weight:
            next_page = page
            break
    return next_page

JINJA_FILTERS = {'prev_page': prev_page, 'next_page':next_page}

and in the theme:

{% set previous = page|prev_page(pages) %}
{% if previous %}previous: {{ previous.title }}{% endif %}

{% set next = page|next_page(pages) %}
{% if next %}next: {{ next.title }}{% endif %}

But... I seriously suggest you to write a plugin to add these attributes to pages. Because you'll be doing this filtering for every page and it can add up. Plugin would be more efficient. You should be able to modify neighbors plugin for your needs.

@steinsag
Copy link
Author

Thank you, that's the way to go!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants