Masatoshi Nishiguchi

Jekyll related page list

I wanted to make a list of related pages at the bottom of each post in my Jekyll site.

Objectives

Jekyll variables

At first, I was a little bit confused about the usage of these Jekyll variables. Actually that is the reason I decided to write this post so that I can save time in the future.

Liquid filters

I heard that Liquid limits its capability for the safety reason. I realized that it was important to clearly understand what we cannot do with Liquid. Also, I need to get used to Liquid’s syntax some of which is very different from Ruby’s.

Pseudo-code

Implementation

{% assign posts_list = "" | split: "|" %}

{% if page.tags %}
  {% for each_tag in page.tags %}
    {% for each_post in site.tags[each_tag] %}
      {% if each_post.title != page.title %}
        {% assign posts_list = posts_list | push: each_post %}
      {% endif %}
    {% endfor %}
  {% endfor %}
  {% assign posts_list = posts_list | uniq %}
{% else %}
  {% assign posts_list = site.posts %}
{% endif %}

<ul class="related-posts">
  {% for post in posts_list limit:include.limit %}
    <li>
      <a href="{{ post.url | prepend: site.baseurl }}">
        {{ post.title }}
      </a>
      <br />
      {% include post_meta.html %}
    </li>
  {% endfor %}
</ul>