How to list categories?

1,978 views
Skip to first unread message

CoolAJ86

unread,
Aug 5, 2010, 10:39:47 PM8/5/10
to jekyll.rb
I'm looking to do this:

Categories:
<ul>
{% for category in site.categories %}
<li>{{ category.title }}:
<ul>
{% for post in category %}
<li>{{ post.title }}: {{ post.url }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>

But I'm stumped as to how to find the name/title/text attribute of
category.

AJ ONeal

unread,
Aug 7, 2010, 12:16:18 PM8/7/10
to jekyll.rb
I'm still looking for an answer to the original question.

Here's another question: Is there a way to do this?

{% for category in sites.categories %}
    {% for posts in category | last %}
    


That said, I've found a few nasty hackish workaround that don't work with jekyll alone (on github, for example):


And a nasty hackish workaround that I created:

---
layout: default
title: Fastr Blog
---
<h2>Categories:</h2>
<ul>
{% for category in site.categories %}
  <li><a href="#{{ category | first }}">{{ category | first }}</a></li>
{% endfor %}
</ul>

<h2>Articles by Category:</h2>
<ul>
{% for category in site.categories %}
  <li><a name="{{ category | first }}">{{ category | first }}</a>
    <ul>
    {% for posts in category %}
      {% for post in posts %}
        <li><a href="{{ post.url }}">{{ post.title }}</a></li>
      {% endfor %}
    {% endfor %}
    </ul>
  </li>
{% endfor %}
</ul>

{% for post in site.categories.quickstart %}
<!-- h2><a href=".{{ post.url }}">{{ post.title }}</a></h2 -->
<!-- {{ post.content }} -->
{% endfor %}

Page generated: {{ site.time | date_to_string }}

Since categories is internally passes `do |category, posts|` it looks like an array where `category | first` is the category name and `category | last` is the array of posts. `category | first` isn't treated as an iterable, so it just gets skipped over in `for post in posts`. So it happens to work, but it's incorrect.

AJ ONeal



--
You received this message because you are subscribed to the Google Groups "jekyll.rb" group.
To post to this group, send email to jeky...@googlegroups.com.
To unsubscribe from this group, send email to jekyll-rb+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jekyll-rb?hl=en.


Reply all
Reply to author
Forward
0 new messages