Skip to content

Instantly share code, notes, and snippets.

@reggieb
Last active December 9, 2015 21:08
Show Gist options
  • Select an option

  • Save reggieb/4328455 to your computer and use it in GitHub Desktop.

Select an option

Save reggieb/4328455 to your computer and use it in GitHub Desktop.
Split a collection of items so that they can be displayed within two column divs
<% number_of_columns = 2 %>
<div class="columns">
<% @things.each_slice((@things.length / number_of_columns.to_f).ceil) do |column_of_things| %>
<div class ="column">
<% column_of_things.each do |thing| %>
<%= thing.title %>
<% end %>
</div>
<% end %>
</div>
@reggieb

reggieb commented Apr 3, 2013

Copy link
Copy Markdown
Author

Using ceil to handle overflow from division, gives more flexibility, and allows the same pattern to be used for 3, 4 or more columns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment