0

I am trying to create a bootstrap group buttons 4 per row until there are no buttons remaining. Static HAML code I want to achive is below (note that btn-group has a dropdown so its quite large underneath)

.btn-group.btn-group-justified
  .btn-group
    ...
  .btn-group
    ...
  .btn-group
    ...
  .btn-group
    ...
.btn-group.btn-group-justified
  .btn-group
    ...
  .btn-group
    ...
  .btn-group
    ...
  .btn-group
    ...
.btn-group.btn-group-justified
  .btn-group
    ...
  .btn-group
    ...
  .btn-group
    ...
  .btn-group
    ...

My current try (which doesn't work) follows:

- some_array.each_with_index do |w,i|
  - if i % 4 == 0
    .btn-group.btn-group-justified
  .btn-group
    ...

Thanks!

zveljkovic
  • 444
  • 4
  • 16

1 Answers1

1

Group the data first using each_slice and the Haml should then be simple:

- some_array.each_slice(4) do |group|
  .btn-group.btn-group-justified
    - group.each do |w|
      .btn-group
        ...
matt
  • 78,533
  • 8
  • 163
  • 197
  • Great! Thank you - that will surely work. I'm just curious, is there another approach as I already had one situation where I had to conditionally put content inside parent or without it? There were no arrays involved just plain variable @nested. – zveljkovic Oct 19 '14 at 18:22
  • @zveljkovic Something like this: http://stackoverflow.com/questions/7237308/how-can-i-conditionally-wrap-some-haml-content-in-a-tag? – matt Oct 19 '14 at 19:38
  • Thank you @matt. I found similar solutions when I was searching but I didn't like the bloat with partials and content_for. Nathan Long solution and info is very nice. Ill check that out. Thank you once again for your help. – zveljkovic Oct 19 '14 at 22:54