Nested fluid loops on the Jekyll archive page do not work. Using an external loop variable inside an internal condition

I work with the creator of the jekyll static site, and I have difficulty in doing the following:

{% for category in site.categories %} <h2 id = "{{ category[0] }}"> {{ category[0] }} </h2> {% for post in site.categories[{{ category }}] %} <li> <a href="{{ post.url }}"> {{ post.title }}</a></li> {% endfor %} <a href="#{{ category[0] }}-ref">&#8617</a> {% endfor %} 

I have a category of messages on my jekyll site called "test", and I can display messages with the following text on it:

 {% for post in site.categories.test %} <li> <a href="{{ post.url }}"> {{ post.title }}</a></li> {% endfor %} 

However, I want to create an archive page automatically and in order to do this. I need to insert a category from the external loop (a loop that visits all categories), and use it in the inner loop to access messages from this particular category. What do I need to do to get the first snippet to work as I want?

EDIT: Alternatively, is there any other way to get the results I want?

+8
ruby jekyll liquid
source share
3 answers

When you execute for category in site.categories ,

  • category[0] will give you the name of the category,
  • category[1] will provide you with a list of posts for this category.

The way Liquid handles iteration over hashes, I reckon.

So, the code you are looking for is the following:

 {% for category in site.categories %} <h2 id="{{ category[0] }}-ref">{{ category[0] }}</h2> <ul> {% for post in category[1] %} <li><a href="{{ post.url }}">{{ post.title }}</a></li> {% endfor %} </ul> <p><a href="#{{ category[0] }}-ref">&#8617;</a></p> {% endfor %} 

I took the liberty of correcting some markup problems - I added <ul>...</ul> around the list of links for communication, <p> around the last link, in the form of a colon after 8617 , and also fixed the id at the top (missing part -ref ).

Hello!

+14
source share

What about...

 {% for category in site.categories %} <h2 id = "{{ category[0] }}"> {{ category[0] }} </h2> <ul> {% for post in site.posts %} {% if post.category == category[0] %} <li> <a href="{{ post.url }}"> {{ post.title }}</a></li> {% endif %} {% endfor %} </ul> <a href="#{{ category[0] }}-ref">&#8617</a> {% endfor %} 

Of course, it is quite inefficient and generates a bunch of extra spaces, but it does its job.

[The original tag was missing. Just added them. In addition, to cover a space, you can collapse everything from for post in site.posts to endfor on one line.]

+1
source share
  {% for post in site.categories.category %} - OR - {% for post in site.categories.category[0] %} 

Also, I'm not sure why the xhep example is not working ...

0
source share

All Articles