0
votes

I have a network of nodes, some of which are related to one another. I'm using Jekyll to power a website, and was hoping to use liquid tags to map those relationships.

So, the way I've been trying to do this is as follows.

A, which is in the category of 1, is related to B B, which is in the category of 2, is related to A, and C

When I visit the page for A, I'd like to see B listed as being related.

I've defined the YAML front matter as such:

title: A
category: 1
tags:
- B.html

title: B
category: 2
tags:
- A.html
- C.html

My liquid template looks like:

<h2>{{ page.title }} <span class="label important">{{ page.category }}</span></h2>
<p>Last edited: {{ post.date | date_to_string }}</p>
<p><em>{{ content }}</em></p>

<h4>Related</h4>
<ul>
 {% for post in site.tag.{{ post.url }} %}
 <li><a href="{{ post.url }}">{{ post.title }}</a></li>
 {% endfor %}
</ul>

To me, that looks like it should work. In reality, it doesn't.

Suggestions are welcome!

Also, relevant Github pages are here: https://raw.github.com/salmonhabitat/salmonhabitat.github.com/master/_posts/2011-12-12-bycatch.md

https://raw.github.com/salmonhabitat/salmonhabitat.github.com/master/_posts/2011-12-12-accidental.md

https://github.com/salmonhabitat/salmonhabitat.github.com/blob/master/_layouts/post.html

What I was intending to happen was for 'Accidental injury' to show up under 'Marine bycatch's' Related nodes...

1

1 Answers

1
votes

The first problem is that posts are basically "blind" to other posts in jekyll. It's impossible to the url (or title) of one post from inside another post in jekyll, with only the id of the former. Your site.tag.{{ post.url }}, while creative, would not work :) .

First, your front matter needs to be (unfortunately) a bit more complicated to be able to accomplish that:

title: A
category: 1
related_posts:
- title: B
  href: /2010/01/01/B.html
- title: C
  href: /2011/11/11/C.html

Notice that I've changed the name from "tags" to "related_posts". I feel it's more clear this way.

Now you can try this code:

<h2>{{ post.title }} <span class="label important">{{ post.category }}</span></h2>
<p>Last edited: {{ post.date | date_to_string }}</p>
<p><em>{{ content }}</em></p>

{% if post.related_posts %}
  <h4>Related</h4>
  <ul>
  {% for related_post in post.related_posts %}
    <li><a href="{{ related_post.href }}">{{ related_post.title }}</a></li>
  {% endfor %}
  </ul>
{% endif %}

While this is a bit more verbose than your version, it has an advantage - you can specify your own title in related posts, without being forced to use B or C.

Let me know if you have problems with this, and good luck!