skwpspace yan pritzker’s home on the web

skwpspace is Yan Pritzker's home on the web

Blog :: Photography :: About Me

TwitterCounter for @skwp

Get the news feed
Get updates by email
Follow me on twitter

hello, i'm yan

This blog is about startups, blogging, Ruby On Rails, virtualization and cloud computing, photography, customer service, marketing, ux and design, git, and lots more.

Top Posts

planypus

I'm the founder of Planypus, the place to share your plans!

cohesiveft

Accessible, manageable, virtualized application stacks ready to download or deploy to the cloud!

flickr

it's hailing in san franciscojessatianalyndaBird in the handkerimodeldanielle pettee and models-4449

Archives

Contact

Reach me at yan at pritzker.ws

Posted
27 September 2006 @ 7am

Tagged
thoughts

Multi column list Rails helper

Of course once you’ve noticed the multi column list pattern is prevalent in your app, it only makes sense to turn it into a helper! Here’s the example from my previous blog post rewritten to use the new helper. Notice how much cleaner it is. Don’t you love Ruby?

Usage:

<%= render_multi_column_list friends do |friend|
      display friend
    end %>

Implementation:

def render_multi_column_list(items, cols=4)
  returning (rendered=“”) do
    items.in_groups_of(items.size/cols) do |group|
      rendered << %{ <div class=’narrow-col’><ul> }
      group.each do |item|
        rendered << “<li>” << yield(item) << “</li>” if item
      end
      rendered << %{ </ul></div> }
    end
  end
end

2 Comments

Posted by
Anton
27 September 2006 @ 3pm

pretty sweet!


Posted by
Florian
8 September 2009 @ 4am

That’s great, but is it possible to pass more variables such as an item id and an item name so you can even make the list link to the individual objects in it?
And for some reason the display function doesn’t work for me, it gives a unknown method error for write #objectid, hoew can I solve this?
Thanks


Leave a Comment

Multi column lists using in_groups_of Rants on google & software dev