Question

I'm using middlemanapp to create a blog. I'm trying to output an archive of blog posts sorted by month and year to display in a sidebar. eg. April 2010, May 2010, June 2010, with clickable links to a archive.

So far have this code below which will output the month in number form (eg. July is being output as 7) and I need to have a list that is displayed by month as shown above.

<% blog.articles.group_by {|a| a.date.month }.each do |month, articles| %>
 <li><%= link_to month, blog_year_path(month) %> </a></li>
 <% end %>

Can someone help, I'm even not certain if middleman offers this functionality, but I'm not very familiar with ruby.

Was it helpful?

Solution

I couldn't find an easy built-in way to do this with Middleman either, but the following will give you a nested list of years and months, with the relevant links:

<ul>
<% blog.articles.group_by {|y| y.date.year }.each do |year, articles| %>
    <li> 
        <a href="<%= blog_year_path(year) %>">
        <%= year %>
    </a>
    <ul>
    <% articles.group_by {|a| a.date.month}.each do |month, month_articles| %>
    <li><%= link_to month_articles.first.date.strftime("%B"), blog_month_path(year, month) %></li>
    <% end %>
    </ul>
<% end %>
    </li>
</ul>

e.g.

  • 2013
    • August
    • July
    • June
    • ...

(I'm fairly sure I borrowed the above from this Middleman template on Github, but if not it was found via a Github search for "blog.articles.group_by month".)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top