1

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.

4

1 回答 1

2

我也找不到使用 Middleman 的简单内置方法,但以下内容将为您提供包含年月的嵌套列表以及相关链接:

<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>

例如

  • 2013
    • 八月
    • 七月
    • 六月
    • ...

(我很确定我从Github 上的这个 Middleman 模板中借用了上面的内容,但如果不是,它是通过 Github 搜索“ blog.articles.group_by month”找到的。)

于 2013-08-29T15:07:22.750 回答