Adding row for each three elements

I want to add row for each three elements. I was using that code with ruby.

@books.each_slice(3) do |three_books| %>
div class= "row ">
<% three_books.each do |book| %>

How can achieve that with phoenix

Maybe one of those Enum functions

chunk_by/2 chunk_every/2 chunk_every/3 chunk_every/4

Probably Enum.chunk_every/2 fits your request


Yes it looks like enum.chunks_every works but i couldnt handle that. It can divide list to 3 elements . I dont know how to handle for each array adding row. Sorry i am really noob. I dont know how ruby magic work behind it

In eex, that would be (not tested)

<%= books |> Enum.chunk_every(3) |> -> %>
  # Do what You want with three books
  <%= three_books |> -> %>
    # Do what You want with one book
  <% end) %>
<% end) %>

Thanks it works.But i didnt understand one point. In first line after chunk_every(3). How behaves?
[1,2,3,5,6] and [[1,2,3],[4,5,6]] For these lists are diffrent?

Yes, they are different…

[1,2,3,5,6] is a list with six elements

[[1,2,3],[4,5,6]] is a list with two lists of 3 elements

1 Like