Helper for generating a tagwise list

13 views
Skip to first unread message

Leslie Viljoen

unread,
Apr 16, 2011, 11:53:18 PM4/16/11
to Webby
Hi everyone

I guess something like this already exists, but I thought I'd share my
helper for generating the tag-wise article list, as can be seen on the
right-hand side bar of my site: http://www.lesismore.co.za

I use it in my layout like so:
<%= tagwise('year', 'entrylist') %>

The two parameters are the css class names to be used for the tag
names and page titles.

It assumes pages with a tag: are also going to have a title:, which it
will use to generate the list.
It goes in the lib directory, in a file called tagwise.rb


module TagwiseHelper
def tagwise(tag_css_class, title_css_class)
tags = Hash.new{|k, v| k[v] = []}

tagged = @pages.find(:all) do |page|
page._meta_data.has_key?("tag")
end

tagged.each {|res| tags[res.tag] << [res.title, res.url]}

tags.sort.map do |tag, links|

links.sort!{|a, b| a[0] <=> b[0]}
links.map!{|tp| %Q[<li><a href="#{tp[1]}">#{h(tp[0])}</a></li>
\n] }
html=<<EOF
<p class="#{tag_css_class}">#{tag}</p>
<div class="#{title_css_class}">
<ul>
#{links}
</ul>

</div>
EOF
end
end
end

Webby::Helpers.register(TagwiseHelper)
Reply all
Reply to author
Forward
0 new messages