-
-
Notifications
You must be signed in to change notification settings - Fork 615
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
Sorting for next_page and previous_page #615
Comments
Hi there. 👋🏻 The If you started with Pico's provided pages_order_by_meta: author # Sort pages by meta value "author" (set "pages_order_by" to "meta")
pages_order_by: alpha # Change how Pico sorts pages ("alpha" for alphabetical order, "date", or "meta")
pages_order: asc # Sort pages in ascending ("asc") or descending ("desc") order The If you set So if you wanted to give each page a hard-coded order, you could set it to Hopefully this helps with what you were looking for. If not, don't be afraid to ask more questions, I'm happy to help. 😉 I'm usually a little more thorough, but I did gloss over a few things in my explanation here that I wasn't 100% positive on, off the top of my head. Including them here just so they don't end up biting you:
I'd usually go spin up my test instance and figure these things out, but they're probably not that important, so I just wanted to get you an answer and not spend all testing pointless tiny details. 😅 I'm always here and always happy to help, so let me know if this gets you going in the right direction. There's no such thing as a stupid question. I try to be detailed enough without sounding condescending (by breaking things down too far) but I'm happy to go into more detail if you need it. 😁 |
I just realized you also specifically asked about sorting by numbered urls. That's probably a bit trickier. I could probably come up with some Twig hackery to sort pages by a known url format (eg the numbers always being in the same place), but it wouldn't affect It's all probably doable, just a bit hacky. Sorting pages automatically based on their url would definitely be something better handled as a Plugin, but that's not my area of expertise (I'm great with Twig... not so much with PHP). 😅 I didn't mean to overlook this in my first comment, I just got caught up writing that explanation. If you decide you really need automatic sorting by url instead of metadata let me know and we can get more hacky (😂). *cracks knuckles* Edit: Apparently I like a challenge too, because now I'm accidentally brainstorming how this could work and it's not actually that crazy sounding. 🤔😂 |
I think sorting with a page meta data will work as long as I number the YAML header with the same number as the URL. But I wish to know that if this change in sorting can be applied to one specific folder rather than site wide. Changes to I am glad to read your extensive reply and enthusiasm. Thank you. |
Ah, okay. You didn't mention the folder thing before. Yes, changes to I've spent a while brainstorming with Twig, but I haven't really come up with a great way to do this. 😓 Can you give me some more context on what you're building / how you'd like it to work? So far I've gathered that: You'd like to have pages that are numbered in their filenames (meaning those numbers will also be shown in the url). You'd like to sort that folder by those filenames. And you only want to use this sorting method in that specific folder? @PhrozenByte would you have any input on this issue? Twig apparently doesn't want to work for me today, as every attempt I've made has kind of backfired and I've been going in circles for awhile now. 😓 |
Sure. Didn't read the full issue, so please forgive me if I'm suggesting stuff that has been sorted out already: Use the Or: Assuming that the page order is correct for all pages except the first and last page within the folder, one could alternatively check whether the prev/next page is within the same folder and, if not, use the Which solution is better depends on the exact use case. Both should work, but personally I'd rather create a plugin. |
Sorry. I didn't know this was important.
Correct. But because I am not using the sorted results anywhere in the site, I think I can mange sorting it site wide and see if
I had tried this though. I am sure there would be solutions if you put enough time into it, but I suggest that you both don't do so. I was hoping for a quick fix like a config setting, hence opened this issue. I will try what I mentioned earlier and if it does not work, I will let it go. However, I wish to raise one inherent problem with the |
I tried this, but I couldn't figure out a good way to get the "next" page via iteration. I was hoping it'd be as simple as grabbing the next page by index ( I suppose I could just store the sorted page id's in an array, and use those (in fact, I just now wrote some test code that does), but that felt like I was heading down the rabbit hole of "too hacky", and that there should probably have been a better way.
Of course you would. 😂 Some of us find PHP to be a headache though, and with the username of "@notakoder", I'm guessing I'm not alone there. 😉
Yeah, there's a number of reasons why that didn't work. I won't go into it in too much detail, but a quick breakdown of the filter pipeline you have there says you're trying to do the following: Get the previous page id. Convert that to the url of the previous page (a single string). Then... sort that single url? ... And reverse it? Yeah, not so good. A for effort though, and I know you found part of that from the "Blogging" example in the docs. It's always good to experiment! I just thought I'd let you know why that wasn't really doing what you thought it might. I think I'm in a better place mentally to help out with this today. I think I can write some more streamlined code for this after all. Is it just the one Do you need to use this sorting from outside of that folder? What I mean by this is, for example, from your main page would you need to know the proper sorting of the
I mean, the You mention that Pico should restrict them to just a particular folder... but Pico's overall philosophy is to not restrict anything. Pico tries to be open and flexible to however a user wants to design their website, and forcing them to use this folder-restricted behavior wouldn't really align with that goal. That's not to say it wouldn't be a neat feature, but it would fall more inline with what a Plugin or Theme should provide. It's a behavior that, while useful to some, would be specific to an individual's use case and not really a good default. Anyway, I'll be back in a little bit with a Twig snippet that replaces Just remember, Pico is incredibly flexible, but that flexibility often comes at the cost of having to provide some of the functionality yourself. Sometimes, you've just got to get your hands dirty and dig into the code. 😉 |
Alright, here's what I've got. I thought it might be handy to put these in sort_directory: articles
sort_meta: Index Put this somewhere before your next and previous links (Thanks to @PhrozenByte for the optimization. 😉). {% if config.sort_directory and current_page.id starts with config.sort_directory ~ "/" %}
{% set sorted_page_ids = pages(config.sort_directory, depth=null)|sort_by(["meta", config.sort_meta], "remove")|keys %}
{% for page_id in sorted_page_ids %}
{% if page_id == current_page.id %}
{% set previous_page = pages[sorted_page_ids[loop.index0-1]] %}
{% set next_page = pages[sorted_page_ids[loop.index0+1]] %}
{% endif %}
{% endfor %}
{% endif %} And if you don't already, you might want your next and previous links in an {% if previous_page %}
<a href="{{ previous_page.id|link }}">Prev Page</a>
{% else %}
Prev Page
{% endif %} |
{% if next_page %}
<a href="{{ next_page.id|link }}">Next Page</a>
{% else %}
Next Page
{% endif %}
We weren't sure whether you wanted subfolders inside ...But making the code work recursively, separating each individual folder level, would be a lot more work, and would probably add additional slowdown that you don't want/need (and definitely not a good task for Twig). And keep in mind that this will only override @notakoder Let me know how this lines up with what you were looking for and I can tweak it however needed. 😁 |
With Twig you have to run the full iteration. Very heavy-handed (or "hacky"), that's the reason why I'd rather create a plugin 🙈
|
Just two small suggestions: Instead of the loop to create {% if config.sort_directory and current_page.id starts with config.sort_directory ~ "/" %}
{% set sorted_page_ids = pages(config.sort_directory, depth=null)|sort_by(["meta", config.sort_meta], "remove")|keys %}
{% for page_id in sorted_page_ids %}
{% if page_id == current_page.id %}
{% set previous_page = pages[sorted_page_ids[loop.index0-1]] %}
{% set next_page = pages[sorted_page_ids[loop.index0+1]] %}
{% endif %}
{% endfor %}
{% endif %} |
Lmao at the timing of your reply. I guess we were both working on it. 😂 Mmhmm, yep, I see. *nod nod*
Or not, I guess, because you liked my approach better (with your simplification). 😂
I started to mention
Absolutely. I often wonder how well some of my Twig code scales. Like how many pages does it take to make this code lag? Could Twig get stuck running so long that the page loading times out? I'm not asking, just being rhetorical. It would be really cool Anyway, @notakoder I'm going to go back and update my example above to match @PhrozenByte's improvements (that way it's all in one place when you come back). |
First of all, my apologies for late reply.
Well, I am way below your league. :D I am literally not a coder, but I understand computer logic and can read and understand some code: that's how I am surviving Pico 😄. I liked the simplicity and performance of Pico, which is why I take the trouble of setting it up over Wordpress.
Corrent. I just didn't want to seem like a guy who didn't try anything at all to fix it. Hence summed it up. 😄
Thank you. But as I said, I wouldn't suggest anyone going through the pain of fixing something as time consuming as this, when I can't compensate you in any manner. But on the other hand, there must be an improvement as well for the
What I am trying to achieve is to apply the Next and Previous links to the contents (not to be confused with the directory name) of a single directory inside the
I experienced two issues: First is with regard to sorting. The filename of the articles inside each folder are numbered as per when it was published. But the default sorting works alphabetically. So 10, 11, and 12 are lesser than 2. If I click the next button on
I assume that there is some more optimisation to be done. So I shall wait before trying out your code. The second issue is more of a problem caused by the templating, which @mayamcdougall already addressed as well. If,
So, I am suggesting that there are two use cases: one that makes sense linking and the other that does not makes sense linking. So if it is possible, Pico should have this 'restriction within a folder' as a configurable option. This will improve the product. I mean, both the solutions to both issues (directory specific sorting and the appearance of next and previous buttons), if technically viable, must be added as a configurable option because such use cases are common. |
That's because computers sort numbers different than humans. However, this is very easy to solve: Use By the way, if you want to get rid of those prefixes in your URLs, try https://github.com/PhrozenByte/pico-file-prefixes
Yeah, I agree, this is a valid feature request. I've just added it to our backlog (#317), but I'm afraid this isn't going to be implemented soon due to very limited time 😒 |
If my understanding is right, Pico or PHP is sorting numbers as strings instead of integers, thus resulting in an alphabetical order. I think it is called lexicographic sorting (forgive me if I am wrong) formally. So my point is, sometimes when numbers are put in the url, they are there for the integer they are. Therefore I should not remove the number with the prefix remover as well. With regard to naming 01, 02..., this restricts the number of files to 99. 001, 002... to 999. One can also prefix the number with more zeros to broaden the max number of files. But I guess, after some point it is just not aesthetical... Consider
That's alright. Glad that it is in. I think @mayamcdougall already gave a better |
No worries, you're not the only one running behind. 😓
(That's why we like it too. 😉)
No, it was done, lol. I just edited my previous comment that way all the code examples (config, sorting, and if links) were all in one place. I didn't want you to have to combine the code from both mine and @PhrozenByte's comment. Either way, it sounds like it doesn't exactly fit yet, since it only supports one sorted folder. It shouldn't be too hard to make it operate on a few different categories (it still won't support any recursion though).
I know it doesn't seem logical to sort them as strings... but its actually a lot harder than you'd think to convert strings to integers. While yes many programming languages support converting strings to integers, you're talking about converting a user supplied string to an integer for sorting. Since the user could name their page almost anything, how do you pick out the integer part and sort by that? Also, you've still got to sort the rest of the characters alphabetically too. Heck, I seem to remember that Windows even struggled with this at some point when it came to file and folder names (pretty sure they fixed it at some point, but I have no idea off the top of my head). Not saying that it's impossible (I mean, there's probably a bunch of libraries out there that could accomplish it too), just that it's not a "simple" problem. Since the intention is that users would probably want to name their page (and their url) something easily readable, it's never really been something worth stressing over. Honestly, if I were doing it, I'd probably skip organizing by index (which would be tedious for a large number of files), and just use leading 0's. Start with however many you think you'll need. Will you have tens, hundreds, or thousands or articles? At a certain point, it becomes unlikely that you'd ever actually need more (will you really need thousands, or is that just wishful thinking?). And, if you ever did cross that threshold, you could just use a batch file renaming tool to add another 0 to everything. (For simplicity, I'll ignore the fact that doing that would break any older links people have made to your pages. I'm more suggesting it would be inconvenient, but not the end of the world.) You can tackle it however you'd like though, that's just my opinion. Though, I understand all too well the uncompromising, perfectionist feeling of "No, it has to be this way!", so I won't judge. 😂
I mean, at that point I think you've got to ask yourself why you're numbering your pages.
I mean... I guess, but it wasn't really the (On a side note, I'm kind of surprised that the first page trying to look up an index of So, the links surrounding the first and last page were just naturally empty. The if statement was just to prevent the Also, by not wrapping the text in an Anyway, sorry for rambling a little here. I'll go add some multi-directory support to that code example.
It's what I do. 😉 I'd like to see Pico's user base grow. If I can keep a Pico user around by walking them through their problems than that's a win. I'd rather help someone solve their Pico use case than have them leave frustrated by it. As long as you've got the patience to work through it, I'm happy to help with any Pico issues I can. ❤️ (Unfortunately, adding new features to Pico itself is solely @PhrozenByte's territory, and he's usually too busy these days to do much work on it. Everything about Pico is done entirely as volunteer work after all! ...and while I'd love to help with Pico's core, I don't know if I have it in me to master PHP just yet. 😂) |
sort_directories:
- technology
- sports
- politics
sort_meta: Index {% set sort_directory = "" %}
{% for directory in config.sort_directories %}
{% if current_page.id starts with directory ~ "/" %}
{% set sort_directory = directory %}
{% endif %}
{% endfor %}
{% if sort_directory %}
{% set sorted_page_ids = pages(sort_directory, depth=null)|sort_by(["meta", config.sort_meta], "remove")|keys %}
{% for page_id in sorted_page_ids %}
{% if page_id == current_page.id %}
{% set previous_page = pages[sorted_page_ids[loop.index0-1]] %}
{% set next_page = pages[sorted_page_ids[loop.index0+1]] %}
{% endif %}
{% endfor %}
{% endif %} This should give it multi-directory support. I couldn't think of any way to combine Also, if you decide you don't want to sort by Let me know what you think and we can take it further from here. 😁 |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in two days if no further activity occurs. Thank you for your contributions! 👍 |
Apologies for the late reply.
I understand. Perhaps this can be tackled as an update in the next release. For now, I'll go with the sorting
My reason was simple, that it is easy to open the nth article by just adjusting the url.
Judging by his previous comment, I believe that he is aware of the issue and have taken a note of it.
Thank you.. 😄 I've been busy lately. I'll try the code for sorting using |
I realised that I can achieve the same order of numbered urls by sorting using the date YAML header. So I added
and removed Also, the list of directory/directories to sort must be configured in multiple lines like your most recent code, than a single line as here.
did not work, but
worked. So anyway, thanks to both of you @mayamcdougall and @PhrozenByte. I hope some of the things we discussed here reflects in the next update. |
No worries about the delay. I've been sick since Christmas with a bad cold (not you-know-what, but still a bit rough). Finally starting to get back into things. Also, don't mind the github-actions bot. It likes to go around threatening to close things. 😉 I think it might be a little too aggressive with it, but most people do tend to leave their issues open when they're done, so... 🤷🏻♀️ Feel free to reopen this issue if you want / whenever you're ready to continue. Or, we can keep discussing on the closed issue, it doesn't really make a difference to me. 😉
I was just trying to be a little funny, saying that by the millionth article, it's gonna look bad with or without those leading 0's. I wasn't trying to be too critical of the pragmatism of that approach, just suggesting that in the real world, you'd probably reconsider how you were labeling them sometime before hitting the seventh decimal place, lol. 😂
Yeah, sorry, I guess I forgot to mention that. It's because the code is expecting an Array now. You could make it more flexible by changing the beginning to something like this: {% set sort_directory = "" %}
{% set sort_directories = config.sort_directories %}
{% if sort_directories is not iterable %}
{% set sort_directories = [sort_directories] %}
{% endif %}
{% for directory in sort_directories %}
... It just does a quick check to see if I've been using this approach lately in my own code to make it more flexible. It does overcomplicate it a little though, so if you can just remember to use multiple lines in your YML, that's probably simpler. 😉 I haven't actually tested that code above, I just wrote it here in the comment box, so probably consider it more of a lesson or a neat trick than an actual example. 😅 Anyway, good luck with your project. Don't be afraid to ask any other questions you might have. 😁👍🏻 |
Well, I am glad to hear you are getting back. Thanks for taking the time to reply nonetheless.
Haha.. yeah, that makes sense.
Yes, and currently that is what I've employed. But glad to see another bit of code that can be experimented with.
Thank you. With you guys available for help, I'm sure things will go just fine. There is one essential refactoring in my opinion, and that is the The ideal aesthetic is to have only the next link on the first article and only the previous link on the most recent article. So I made the following modifications to the code.
The problem is the pipe character, which must not appear unless both next and previous links are rendered. And I understand why it appears otherwise—it is not a part of the if condition. You cannot add |
No, but the area around the
Since you only want it to appear if both {% if previous_page and next_page %} | {% endif %} |
That's a simpler fix! |
Is there any sorting methods that we can specify for
next_page
andprevious_page
twig variables?Eg, data/time of the article or perhaps treating the url as an integer to sort numbered urls properly. Currently it is alphabetical or lexicographic.
The text was updated successfully, but these errors were encountered: