Xirux Nefer
Xirux Nefer

Reputation: 830

Printing tag in Jekyll prints entire post and other problems

So basically I'm having this problem. I want to print the categories name as a heading, but categories with several words haven't work so far with anything I've tried, so I've solved it setting a tag and a category in every post.

So after this decision, I tested 2 tags and 2 categories, tag1 for cat1 and tag2 for cat2, where every tag has a correspondent category. Then I added those tag/cat pairs to some of my posts. My algorithm looks like this:

for loop in tags

    print category in a heading tag

        for loop in posts of that category

            print some post info

which, translated into code, looks like this:

{% assign i = 0 %}
{% for TAG in site.tags %}
    <p>Checking the i: {{ i }}</p> 
    <p>Checking the tag: {{ TAG }}</p> 
    <h1>Checking the cat: {{ site.categories[i] }}</h1> 

    {% for post in site.tags.TAG %}
        <p>{{ post.title }}</p>
    {% endfor %}
    {% assign i = i + 1 %}
{% endfor %}

And the output is:

Checking the i: 0
Checking the tag: tag1
THE WHOLE POST
Checking the cat: (nothing is printed here)

Checking the i: 0
Checking the tag: 0
Checking the cat: (nothing is printed here)

And then it stops.

So my questions are:

Upvotes: 0

Views: 493

Answers (1)

David Jacquel
David Jacquel

Reputation: 52809

How to access the category? Right now it prints nothing

Site categories is a hash :

Hash
{"github issue"=>[#Jekyll:Post, #Jekyll:Post],
 "toto"=>[#Jekyll:Post, #Jekyll:Post],
 "jekyll"=>[#Jekyll:Post]}

site.categories['github issue'] return a post array site.categories[0] return an empty array

Why does it print the WHOLE post contents after the tag?

Same reason as above in the {% for TAG in site.tags %}, TAG is a hash an what you see is the string representation of this hash. If you have two post in this tag, you will see two post when you print TAG.

To get the tag name from here : {{TAG.first}}

Why does it print one tag only?

It prints two tags

Checking the i: 0
Checking the tag: tag1
THE WHOLE POST
Checking the cat: (nothing is printed here)

Checking the i: 0
Checking the tag: 0
Checking the cat: (nothing is printed here)

This behavior is clearly unpredictable mainly because you've declared tags and categories in your _config.yml. You are not supposed to do that because site.tags and site.categories are set by Jekyll at generation time depending on tags and categories found in your posts. Remove this from your config and only set you tags in default front matter config or in posts.

Why isn't the i incremented at the end of the loop?

{% assign i = i | plus: 1 %}

See liquid documentation

Possible solution

{% for tag in site.tags %}
  {% assign tagName = tag.first %}
  <h1>{{ tagName }}</h1>
    {% for category in site.categories %}
    <h2>{{ category.first }}</h2>
        {% for post in site.posts %}
          {% if post.tags contains tagName %}
              <h3>{{ post.title }}</h3>
          {% endif %}
        {% endfor %}
    {% endfor %}
{% endfor %}

Upvotes: 1

Related Questions