Last Updated: February 25, 2016
· ofcapl

Playing with tagged items order in nanoc


As I wrote earlier, I'm using nanoc to generate my site - it's made from pure static files, no database here.
Nanoc provides several plugins/libraries to enhance its own capabilities. One of them is Tagging library.

How does it work?

At first, let nanoc know that You want use Tagging library - to do it add this into Your lib/default.rb file:

include Nanoc3::Helpers::Tagging

Pure Tagging library saves us time group our articles - to simply add a tag we have to define it in the item header:

kind: article
created_at: 1 January 2013
title: My fancy article
tags: ['blog', 'mobile', 'tech']

Then we can use it in our layout file like this:

- items_with_tag('my_tag1').each do |post|
    = link_to(post[:title], post.path)

But there is one little problem - it sorts founded items from the oldest to the newest ones - very often we want to make it work in opposite way.


As You should know, we can add/make libraries for nanoc in lib/default.rb file. Let's write own little one:

def tagged_articles(type)
  items_with_tag(type.to_s).sort_by do |a|

This should solve our problem with displaying order of the tagged items - it will display them from the lately created to the oldest ones (You can change this at the third line, e.g. to display them alphabetically). Here's an example usage:

- tagged_articles('mobile').each do |post|
    = post[:created_at]
    = link_to(post[:title], post.path)

Our function will display articles about mobile. This solution is really great to categorize Your items - e.g. at Webrackets site I've categorized elemets to blog posts and project's case studies items.

I hope my solution will help someone - if so, make me happy and tweet to me.

-- ofca