kolber / stacey

Cheap & easy content management
http://staceyapp.com
MIT License
1.04k stars 131 forks source link

Examples of sort twig extensions in wiki? #135

Closed kelon closed 9 years ago

kelon commented 9 years ago

Hi. Would it be possible to add a quick example of how to use the twig extensions: sortbydate(), sortby() and custom_str_sort() please? I see they're there and I've looked at the twig-extensions.inc script but it's proved a bit beyond me. I've messed about trying to get them to work, researched twig documentation about extensions and failed to grasp it. I'm not sure what I'm doing wrong and an example would be really helpful. If you've only time to do one, can I ask you to do the sortbydate() function, as it's the one I'm desparate for? Thank you.

Edit: I'd like to sortbydate page.children

Edit: Hum. Been playing with this. I'm loading in all articles from sub-directories into a single list as though they are all the same level:

<li><a href="/11.SOMEdir/article-middle">article January 15</a><li> <li><a hred="/15.OTHERdir/article-bottom">article December 14</a></li> <li><a hred="/21.EXTRAdir/article-top">article March 15</a></li>

The articles display in the order their parent directories are numbered in. I'd like to display most recent article first regardless of its containing directory's order.

Digging about I see that Stacey does not construct a complete array of all pages (when drawing on multiple directories at once?) - presume this would be too slow once a lot of articles exist - so it's not possible ? Please confirm there is no way to do this.

I've seen a 'teaser' snippet in the snippets area which would enable me to do it for this one page, though it's a bit of a manual maintenance, so unrealistic to use over several sections of a site. I could also renumber the directories each time and draw only one article from each. Just trying to think my way round this as Stacey is a great app that I enjoy using on my site.

I'm curious what the sortbydate() function in the extensions folder is intended for, so an example would still be really useful

pbatts commented 9 years ago

Hello, Try this:

{% for sibling in sortbydate(page.siblings, 'updated') %}
  <p>{{ sibling.title }}</p>
 {% endfor %}

Also, I found this helpful. https://github.com/kolber/stacey/search?utf8=%E2%9C%93&q=sortbydate

pbatts commented 9 years ago

Did that help? Did you get your project sorted out? If so please close this issue.

kelon commented 9 years ago

Hi, apologies, I've not had chance yet to try it out, been up to my ears in other projects. I will have a go and provide feedback. It does look like it could be just what I'm hoping for so thank you, taking the time to suggest something is very kind and appreciated.

kelon commented 9 years ago

Hi. Okay, so it works sorting files on file modified date. I was hoping I could sort, based on file created date. I don't understand the functions well enough to know if it's built in (I had a punt at passing 'created' instead of 'updated'). This was why I asked if there could be more info in the Wiki.

kelon commented 9 years ago

Ah, 'updated' is in the page array page.updated ... of course that's where it is. I was being thick as usual. Now I see. I'll need to code the date into the yml file in order to access a page.created value... I presume. I'm guessing this is because file created timestamps are not always trustworthy...