-2

I'm having an HAML issue writing an if else statement ...

I have this code :

- experiences.each_index do |index|
  - if index % 2 == 0 ?
    .group
      .left
  - else
    .right
  %p= experiences[index].company

And I would like to produce that kind of html:

<div class='group'>
  <div class='left'></div>
  <div class='right'></div>
</div>

Multiple times depending on my collection's lenght.

Any idea ? Thanks

EDIT> A workaround would be to do this :

  - experiences.each_index do |index|
  - if index % 2 == 0 ?
    .group
      .left
        experiences[index]...
      .right
        experiences[index+1]...

But isn't there any better solution ?

Vincent Prigent
  • 119
  • 1
  • 6

1 Answers1

3

You could use in_groups_of:

- experiences.in_groups_of(2) do |left, right|
  .group
    .left
      %p= left.company
    .right
      %p= right.company if right
spickermann
  • 100,941
  • 9
  • 101
  • 131