Note: this was tested in Nanoc 3.6.1
I’ve seen a few questions (and a few incorrect answers) about how to get next and previous links for a given item in Nanoc. If you are using the blogging module in Nanoc you can access all your articles in sorted order by descending creation date. Using this we can create a quick helper for next and previous links.
Add the following to your helpers, usually
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
def previous_link prev = sorted_articles.index(@item) + 1 prev_article = sorted_articles[prev] if prev_article.nil? '' else title = prev_article[:title] html = "← Previous" link_to(html, prev_article.reps, :class => "previous", :title => title) end end def next_link nxt = sorted_articles.index(@item) - 1 if nxt < 0 '' else post = sorted_articles[nxt] title = post[:title] html = "Next →" link_to(html, post.reps, :class => "next", :title => title) end end
It’s important to note that
sorted_articles returns an array where
sorted_articles is the newest article. So if you want to get the next (newer) article you want to decrement the current index.
Once you have this in a helper module, you can use it in your layout with
previous_link. My post layout is in Haml so it looks like:
1 2 3 4 5 6 7
%article.post = yield %p.prev = previous_link %p.next = next_link
I use this on this blog, and you can check out the full source on github.
blog comments powered by Disqus