Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I loop through tags in a jekyll post?

Tags:

jekyll

Within my post, I have three tags in my front matter, I am trying to loop through these tags but it just puts all three of them mashed together in one string.

I am using this:

<tr>
{% for tag in post.tags %}
<td>{{ post.tag }}</td>
{% endfor %}
</tr>

I'd like an out of the box solution instead of relying on plugins but I am hosting my own so I am able to use them if I have to.

like image 560
motleydev Avatar asked Aug 09 '12 16:08

motleydev


2 Answers

You are referencing page.tags instead of just tag in your loop. Use the following:

<tr>
{% for tag in page.tags %}
<td>{{ tag }}</td>
{% endfor %}
</tr>

Read more in the documentation.

like image 58
rudolph9 Avatar answered Nov 11 '22 22:11

rudolph9


Ok, I finally figured it out. This is not really covered in the documentation very well but makes ALL the difference. Jekyll supports BOTH tag: x, y, z AND tags: x, y, z - the s is very important. That changes whether or not jekyll will interpret multiple values or a single - the same problem can be found with category -> categories in the front matter.

I likely missed it because I was using:

tag:
- x
- y
- z

Which is also supported but would not return string literal of "x, y, z" which might have ben a give away. Anyways, Thanks to rudolph9 for at least validating that my general direction was correct and for the syntax catch. I'm marking his as the right answer (cause I'm a nice guy) but you need to read this one as well to get the full learning McMeal.

like image 36
motleydev Avatar answered Nov 11 '22 21:11

motleydev