Archives

9 views
Skip to first unread message

Noah K Tilton

unread,
Jun 5, 2008, 2:31:41 AM6/5/08
to na...@googlegroups.com
Hi,

My question is how to implement archives?  Anyone have ideas on how to do this from experience? 

e.g., My site is organized as /$year/$month/$date.  I would like /$year and /$month to display an index of posts in that timeframe.  Any ideas?

Thanks,
Noah

Michael McDermott

unread,
Jun 5, 2008, 3:29:04 PM6/5/08
to na...@googlegroups.com
Version it in Subversion or your favorite VS system.

Denis Defreyne

unread,
Jun 5, 2008, 4:08:44 PM6/5/08
to na...@googlegroups.com
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

Noah K Tilton

unread,
Jun 5, 2008, 4:13:56 PM6/5/08
to na...@googlegroups.com
On Thu, Jun 5, 2008 at 3:08 PM, Denis Defreyne <denis.d...@stoneship.org> wrote:
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

Denis,

This is precisely the type of information I was looking for.

Thanks!

Noah

Reply all
Reply to author
Forward
0 new messages