2

My index page looks like:

.subfeatures.row
  - @collection_pages.each do |collection_page|
    %div{class: "subfeature-0#{collection_page.position} columns four"}
      = link_to collection_page do
        .img
          = image_tag collection_page.cover_image
          .content
            %h3.title
              = collection_page.link
            %hr
            %p.description View Collection >

Given that I have 5 items. This is generating:

    <div class="sub features row">
      <div class="subfeature-01 columns four">
      <div class="subfeature-02 columns four">
      <div class="subfeature-03 columns four">
      <div class="subfeature-04 columns four">
      <div class="subfeature-05 columns four">
   </div>

I need it to generate the following somehow, pretty much each row can contain 3 items:

  <div class="sub features row">
    <div class="subfeature-01 columns four">
    <div class="subfeature-02 columns four">
    <div class="subfeature-03 columns four">
  </div>

  <div class="sub features row">
    <div class="subfeature-04 columns four">
    <div class="subfeature-05 columns four">
  </div>

Is there a good way to accomplish this?

Thank you in advance!

4
  • You haven’t closed your div elements in your HTML, did you mean to do so? Commented Jul 18, 2014 at 16:01
  • I was just using it as an example. Commented Jul 18, 2014 at 16:25
  • but is what you’ve put the result you actually want to achieve, or should the inner divs all be closed and the first sub features row div be closed before the second is open? The answer will depend on that. Commented Jul 18, 2014 at 16:28
  • ahh sorry about that, I made the edits! Commented Jul 18, 2014 at 17:21

2 Answers 2

6

Use each_slice to organise the data first, then the structure of the Haml will be simple:

- @collection_pages.each_slice(3) do |slice|
  .subfeatures.row
    - slice.each do |collection_page|
      %div{class: "subfeature-0#{collection_page.position} columns four"}
Sign up to request clarification or add additional context in comments.

Comments

0
.subfeatures.row
  - @collection_pages.each_with_index do |collection_page, idx|
    if idx % 3 = 0
      %div{class: "subfeature-row"}
    end
    %div{class: "subfeature-0#{collection_page.position} columns four"}\

You can instead use each_with_index. For everytime that it equals 0 when it is divided by 3, I create a div class "sub-feature row"

4 Comments

Are you talking about the missing end?
idx is an index, not a count: on the first iteration, idx is equal to 0, not 1; on the third iteration, idx is equal to 2, not 3. idx % 3 == 0 will not work as expected, use if (idx+1) % 3 == 0 instead
That is what I get for editing someone elses post, and not looking at it in depth
@Mandeep no it should not be idx % 4 == 0: Try in your IRB console (0..12).each{ |n| puts n if n%4 == 0 }, it is showing every 4th element, not every 3rd.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.