Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll site.categories.{{variable}}?

Tags:

jekyll

liquid

I want to make an archive page with the example generator from the Jekyll documentation. The generator works fine but I don't know to implement the layout correctly. I am using the following file so far:

{% assign cat = page.category %}
<div class="category-archive">
  <div>
    <span class="title">Category archive for {{ cat }}</span>
  </div>
  <div>
    {{ cat }}
    <ul class="posts">
      {% for post in site.categories.cat %}
      <li><span>{{ post.date | date_to_string }} - </span> <a href="{{ post.url }}">{{ post.title }}</a></li>
      {% endfor %}
    </ul>
  </div>
</div>

How can I use the current category from page.category like with a variable I am trying to use here?

TL;DR
I want to use a liquid variable at site.categories.*

like image 800
Kageetai Avatar asked Mar 16 '14 18:03

Kageetai


2 Answers

I figured it out myself!

The line
{% for post in site.categories.cat %} can be written like:
{% for post in site.categories.[page.category] %}

It didn't know about the use of these brackets!

like image 97
Kageetai Avatar answered Sep 25 '22 22:09

Kageetai


The correct syntax for the loop is

{% for post in site.categories[cat] %}
like image 31
David Hutchison Avatar answered Sep 25 '22 22:09

David Hutchison