The first thing you should do, is make sure that all articles have a
post date/time associated with them. Something like this in the
meta.yaml:
created_at: "2008-04-17 20:30:00"
(metadata attributes ending with _at or _on will be converted to Time
resp. Date objects.)
Now you can make an archive page. In the meta file, make sure the page
gets run through ERB before it is laid out:
filters_pre: [ 'erb' ]
... and in the page itself, put something like
<% articles_from_this_year = @pages.select { |page| page.kind ==
'article' and page.created_at.year == year }.sort_by { |page|
page.created_at }.reverse %>
<% articles_from_this_year.each do |article| %>
...
<% end %>
This is for year archives, but the code for month archives is similar.
It's also probably better/cleaner to put this in a .rb file in the lib
directory. For example, for the Stoneship web site I have this:
def articles_for_year(year)
@pages.select { |page| page.kind == 'article' }.
select { |page| page.created_at.year == year }.
sort_by { |page| page.created_at }.
reverse
end
Also, because year/month archives are all very similar, you may want
to create 'year_archive' and 'month_archive' layouts that contain
something like this:
<% articles_for_year(@year).each do |article| %>
<h2><%= article.title %></h2>
<p><%= article.excerpt %> <a href="<%= article.path %>">Read more</
a>.</p>
<% end %>
To use this layout, put this in your year/month archive pages:
<%= render('year_archive', :year => 2008) %>
Hope this helps you get started!
Regards,
Denis
--
Denis Defreyne
denis.d...@stoneship.org
Hi,
The first thing you should do, is make sure that all articles have a post date/time associated with them. Something like this in the meta.yaml:
created_at: "2008-04-17 20:30:00"
(metadata attributes ending with _at or _on will be converted to Time resp. Date objects.)
Now you can make an archive page. In the meta file, make sure the page gets run through ERB before it is laid out:
filters_pre: [ 'erb' ]
... and in the page itself, put something like
<% articles_from_this_year = @pages.select { |page| page.kind == 'article' and page.created_at.year == year }.sort_by { |page| page.created_at }.reverse %>
<% articles_from_this_year.each do |article| %>
...
<% end %>
This is for year archives, but the code for month archives is similar. It's also probably better/cleaner to put this in a .rb file in the lib directory. For example, for the Stoneship web site I have this:
def articles_for_year(year)
@pages.select { |page| page.kind == 'article' }.
select { |page| page.created_at.year == year }.
sort_by { |page| page.created_at }.
reverse
end
Also, because year/month archives are all very similar, you may want to create 'year_archive' and 'month_archive' layouts that contain something like this:
<% articles_for_year(@year).each do |article| %>
<h2><%= article.title %></h2>
<p><%= article.excerpt %> <a href="<%= article.path %>">Read more</a>.</p>
<% end %>
To use this layout, put this in your year/month archive pages:
<%= render('year_archive', :year => 2008) %>
Hope this helps you get started!
Regards,
Denis
--
Denis Defreyne
denis.d...@stoneship.org