-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Comments
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 :) |
@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:
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. |
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 |
something like this, you probably need to play around with it a bit:
if that works you still need to determine the index of the current page and select the next/prev one |
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? |
@steinsag you can use 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. |
Thank you, that's the way to go! |
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?
The text was updated successfully, but these errors were encountered: