Masatoshi Nishiguchi
Software Engineer

Jekyll related page list

jekyll

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

My goals

Jekyll variables

At first, I was a little bit confused about how to use 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 filters purposely has limitted capabilities for the safety reason. I realized that it was important to clearly understand what we can and 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 | absolute_url }}">
        {{ post.title }}
      </a>
      <br />
      {% include post_meta.html %}
    </li>
  {% endfor %}
</ul>