1
votes

I am new to Jekyll, and I am working on a site where I want to have a navigation menu that uses the category names as the link text. However, I don't want the cat names ordered alphabetically or reversed, but in a different order. The only thing I could come up with is, defining a hash in the config file like this:

cats:
  "a": "dogs"
  "b": "cats"
  "c": "spiders"
  "d": "jiraffes"

and then for the navigation I have something like this (please don't laugh at this noobie, he he):

<ul>{% for cat_hash in site.cats %}{% for cat in cat_hash %}{% for page in site.pages %}{% if cat[1] == page.category %}
    <li><a href="{{ page.url | prepend: site.baseurl }}">{{ page.category }}</a></li>{% endif %}{% endfor %}{% endfor %}{% endfor %}
</ul>

Now, since I have many pages under each category, I would like to automate the process a bit, so I'm trying to use liquid code in the front matter like this:

---
layout: default
category: {{ site.cats["a"] }}
---

but of course this doesn't work. I've searched SO and found a solution using a plugin, but I can not use plugins for this site. Anybody has any idea? What I would like to do is:

  • Have the categories sorted in any order I want, (not alphabetically).
  • Automate the cat name generation in the front matter

Thank you in advance.

1
I've been trying to do that too, with no luck. Sometimes Jekyll is so manual and limited. Gonna keep checking this question out.Xirux Nefer

1 Answers

2
votes

A possible solution :

Ordering is not the problem and I think that the _config.yml seems to do it.

The problem is to automatically match a page to a category without having to write the category name in the pages's front matter.

My idea is then to match a category to a folder. Any file present in the cats folder will be considered to be part of the cats category, and then appear in the right menu.

--cats
  |--cat1.md
  |--cat2.md
  |--
--dogs
  |--dog1.md
  |--dog2.md
  |--
--spiders
  |--spider1.md

Then the _config.yml can be changed a little to give a match between folder name and display in the menu.

categories:
    dogs:
        display: Doggies
    cats:
        display: I love catz
    spiders:
        display: Spiders

Now we can easily match our pages to a category and display everything in a menu :

{% for cat in site.categories %}
  <h2>{{ cat[1].display }}</h2>
  <ul>
  {% for page in site.pages %}
    {% if page.dir contains cat[0] %}
      <li><a href="{{ page.url | prepend: site.baseurl }}">{{ page.title }}</a></li></li>
    {% endif %}
  {% endfor %}
  </ul>
{% endfor %}

Et voilà !