Skip to content

Instantly share code, notes, and snippets.

@reggieb
Last active December 9, 2015 21:08
Show Gist options
  • Save reggieb/4328455 to your computer and use it in GitHub Desktop.
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
Copy link
Author

reggieb commented Apr 3, 2013

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