bobmcwhirter/awestruct — Issue #45: helper for generating tag links for a post
Author: mojavelinux
Created: 2011-11-03 06:11:21+00:00
State: open

Generating links to the tags of a post is very verbose, especially in haml. Additionally, there is a shortcoming in haml that makes it difficult to put a delimiter after a tag with the proper spacing.

Here's the haml that I currently use for a post:

- post.tags.each do |tag|
%a{:href=>tag.primary_page.url}>= tag
- if not tag == post.tags.last
= ', '

That's just plain nasty to read. (Btw, the entity after the comma represents a normal space. It's required because otherwise haml would strip the space after the comma).

Here's the helper that I'm proposing:

def tag_links(tags, delimiter = ', ', style_class = nil)
class_attr = style_class ? ' class="' + style_class + '"' : ''
tags.map{|tag| %Q{#{tag}}}.join(delimiter)
end

I'm just not sure where to stick it. It seems it should go in the Tagger extension. It could be a module named TagLinker, and all post pages would extend it.