No way to order post or pages #42

Closed
plusjade opened this Issue Feb 13, 2012 · 15 comments

Projects

None yet

9 participants

@plusjade
Owner

I notice people search for how to order page and post lists in Jekyll Bootstrap. Just so there is a place where this question is answered:

There is currently no way to pragmatically order pages or posts.

The posts are ordered chronologically, while the pages are ordered alphabetically (to my knowledge).

The only way to create your own order is to manually create an array in the _config.yml or YAML Front Matter pointing to the post/page urls, then iterate over that array.

This is one of the biggest drawbacks to sticking with the Jekyll framework. I'm currently scheming a solution for this =)

@ghost

The only way to create your own order is to manually create an array in the _config.yml...

Some time ago I did this for category names. It works locally but it's not an option when using Github pages. In Github it seems that _config.yml is updated by merging any new change with the old values. When using {{ site.array }} the order is not guaranteed to be the same as in _config.yml

@plusjade
Owner

@abyme that is a very good point and also explains why that happend to me a couple times =).

This makes it even harder to get proper ordering in Jekyll =/

@ghost

It's not possible without plugins as far as I know. I currently list categories manually in a included template.

There is a way (at the end of that post) to do it, but it's possibly the worst hack ever.

@FlorianWolters

Concerning Github Pages: It is definitely the timestamp of the static .html files that are created by Jekyll which determines the ordering.

I wanted to order my website navigation but still use the following Jekyll-Bootstrap snippet (so that the default.html file of my theme remains generic):

{% assign pages_list = site.pages %}
{% assign group = 'navigation' %}
{% include JB/pages_list %}

I tried the following to achieve an ordering:

  1. Renamed all files which should be included in the navigation, e.g. 00-home.md, 01-about.md, etc.
  2. Tested the navigation output on my local computer with jekyll --server. => The navigation has the correct order.
  3. Pushed it to GitHub and tested the navigation output. => The navigation has a random order.

So, is there any possibility to get a custom ordering of the navigation and pages? It is definitely a huge drawback.

The only solution I see is to remove the Jekyll-Bootstrap code above and hardcode the navigation within the default.html file of my theme. But that isn't a good solution too, since themes should be as generic (and therefore usable for others) as possible.

@plusjade
Owner

@FlorianWolters yep I agree, it is very annoying not being able to preserve order.

You may or may not really like this answer but after spending much time working on Jekyll Bootstrap, the next step in its evolution is ...

re-design how a static blogging generator can work. http://ruhoh.com is my project, it naturally supports ordered lists. The neat thing about ruhoh is all posts and pages have ids. Throughout the system you can reference the post/page by id, for example create an array of page ids, then the templater is smart enough to expand the id to the relevant post/page object.

It's outlined here: http://ruhoh.com/usage#to_pages

@FlorianWolters

@plusjade I appreciate your fast answer and your work on both static blogging generators.

I definitely like your answer and I will give Ruhoh a try.

Can you tell me something about the future of Jekyll-Bootstrap? Will you continue both projects or will Ruhoh supersede Jekyll-Bootstrap and the latter will be discontinued?

Nonetheless, you should clarify the future of Jekyll-Bootstrap on http://jekyllbootstrap.com.

@plusjade
Owner

I will be committed to maintaining JB but work will be relegated to weekends mostly.

I created JB from my initial frustrations working with Jekyll. Most people praise Jekyll's ease of use but for me it took a lot of effort to grok how and why Jekyll was doing what it was doing. The biggest pain point for me as a programmer is shoehorning my mind into Liquid. I get why liquid is useful, but to a programmer on his own machine, it's blasphemy.

Jekyll is great because it popularized and solidified static blogging as a viable alternative. This is of course because Jekyll is made by GitHub and natively supported by GitHub.

Moving forward, I reflected on why I made JB in the first place. It was to help myself blog more. I've had many Wordpress sites, a posterous site, a sinatra site, and they all achieve the same thing - nothing. I never write in them. I realized the workflow was not motivating to me.

If my goal is to help people blog more, then prioritizing Jekyll-Bootstrap development has on fatal flaw -

no matter how feature-rich, intuitive and awesome JB gets, it will only ever be useful to ruby programers.

What to do?

There are countless other static blog generators out there, so I have to ask myself is making another one a good idea?

http://ruhoh.com is the only one I know of built to be language-agnostic. The api is heavily influenced by Jekyll and I'd argue using Mustache for templating is 54.67 trillion times better and easier than using Liquid. Mustache is of course supported in many languages

So to answer your question officially - I think the best use of my time, relative to my goal of empowering all developers to share their knowledge through blogging, is to commit to building a language-agnostic Universal Blogging API.

Lastly, I definitely agree about making my intentions clear on jekyllbootstrap.com. I'm just finishing up my hosting service for ruhoh, so as soon as I get the ruhoh.com site a bit more officialized, I'll update JB with a well-intentioned outline of the future of both products.

@FlorianWolters I appreciate the relevant questions and could use some feedback on what you think from a user perspective? Are you interested in trying out ruhoh's hosted service? username.ruhoh.com, just like GitHub - preview here

@balta2ar

I thought maybe one can add another tag to the header of .md file. Similar to group, but name it order and set to 1, 2, 3 etc according to the order you want your navigation to appear. Then in _includes/page_list you need to sort pages_list variable by that order. The only problem is that I don't know how to specity the field to use for comparison.

EDIT: found solution here: http://stackoverflow.com/questions/9053066/sorted-navigation-menu-with-jekyll-and-liquid

@crazymaster crazymaster referenced this issue in moto-net/moto-net.github.com Dec 2, 2012
Open

ナビバーの順番が変わる #6

@WojtekKruszewski

This works for me:

{% assign sorted_pages = site.pages | sort:"name" %}
{% for node in sorted_pages %}

Then add numerical prefixes to file names to order them.

Optionally fix permalinks:

---
layout: default
title: News
permalink: "index.html"
---

http://stackoverflow.com/questions/13266369/how-to-change-the-default-order-pages-in-jekyll/16625558#16625558

@solkar

I wanted to use the custom array solution, and I struggled to find out the fields needs. Finally I came out with this,

in _config.yml, add:

custom_navigation_bar:
 - {title: foo_title1, group: navigation, url:bar_url1}
 - {title: foo_title2, group: navigation, url:bar_url2}

user your custom array inside default.html:

    <div class="navbar">
      <div class="navbar-inner">
        <div class="container-narrow">
          <a class="brand" href="{{ HOME_PATH }}">{{ site.title }}</a>
          <ul class="nav">
            {% assign pages_list = site.custom_navigation_bar %}
            {% assign group = 'navigation' %}
            {% include JB/pages_list %}
          </ul>
        </div>
     </div>
   </div>
@marshallshen
Collaborator

@snowdream 👍
Great idea, mind to submit a PR for a JB pluging?

@groundh0g
Collaborator

I think something like this should work. I tested it locally and all seems well.

JB/sort_collection

{% capture jbcache %}{% comment %}

  Sort the given array or map.

  Parameters:
    collection: the array or map to sort [REQUIRED]
    sort_by: the property to sort by [OPTIONAL]
    sort_descending: reverse the collection [OPTIONAL]

  Returns:
    sort_result: the sorted collection

  Examples:
    <h3>Pages</h3>
    <ol>
      {% include JB/sort_collection collection=site.pages sort_by="title" %}
      {% assign pages_list = sort_result %}
      {% include JB/pages_list %}
    </ol>

    <h3>Pages [Reversed]</h3>
    <ol>
      {% include JB/sort_collection collection=site.pages sort_by="title" sort_descending=true %}
      {% assign pages_list = sort_result %}
      {% include JB/pages_list %}
    </ol>

    <h3>Array</h3>
    <ol>
      {% assign test_array = "one,two,three,four" | split: "," %}
      {% include JB/sort_collection collection=test_array %}
      {% for test in sort_result %}
        <li>{{test}}</li>
      {% endfor %}
    </ol>

    <h3>Array [Reversed]</h3>
    <ol>
      {% assign test_array = "one,two,three,four" | split: "," %}
      {% include JB/sort_collection collection=test_array sort_descending=true %}
      {% for test in sort_result %}
        <li>{{test}}</li>
      {% endfor %}
    </ol>

{% endcomment %}

{% assign is_array = true %}
{% assign sort_result = "," | split: "," %}
{% assign collection = include.collection %}
{% if include.sort_by %}
  {% assign sort_by = include.sort_by %}
{% else %}
  {% assign sort_by = "title" %}
{% endif %}

{% if collection and collection.size > 0 %}
  {% for x in collection.first %}
    {% if x[1].size > 0 %}
      {% assign is_array = false %}
    {% endif %}
    {% break %}
  {% endfor %}

  {% if is_array == false %}
    {% assign sort_result = collection | sort: sort_by %}
  {% else %}
    {% assign sort_result = collection | sort %}
  {% endif %}

  {% if include.sort_descending %}
    {% assign reversed = "," | split: "," %}
    {% for index in (1..sort_result.size) %}
      {% assign i = sort_result.size | minus: index %}
      {% assign reversed = reversed | push: sort_result[i] %}
    {% endfor %}
    {% assign sort_result = reversed %}
    {% assign reversed = nil %}
  {% endif %}

{% endif %}{% endcapture %}{% assign jbcache = nil %}

I plan to create a PR for this tonight. Offering up for peer review.

EDIT: Updated code to support reversed sorts. Probably not going to issue PR tonight. But soon. So comment if you see something funky.

@groundh0g groundh0g self-assigned this Mar 2, 2015
@groundh0g groundh0g pushed a commit that referenced this issue Mar 3, 2015
groundh0g Addresses issue #42. Allows sorting of collections: arrays or hashes,…
… ascending or descending.
4eebb44
@groundh0g groundh0g added feature and removed request labels Mar 3, 2015
@groundh0g groundh0g added this to the v 0.4.0 milestone Mar 15, 2015
@groundh0g
Collaborator

Merged. Low risk. Stand-alone helper file.

@groundh0g groundh0g closed this Mar 15, 2015
@knoxxs knoxxs added a commit to knoxxs/knoxxs.github.io that referenced this issue Aug 27, 2015
@knoxxs knoxxs corrects page order 1f833f8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment