1

I'm new to Ruby and Ruhoh and have I am trying to do something like "Rails each loop insert tag every 6 items?" but I am using Ruhoh.

Basically, I have a list of posts and every 3 posts I want to create a new row div.

I have looked through all the Ruhoh documentation and there doesn't appear to be an easy way to do this. I think I need to create a plugin in Ruhoh for a collection, but having no experience in ruby I don't really understand what I am doing. Any help or guidance in the right direction would be great,

Cheers.

Community
  • 1
  • 1
user3277112
  • 157
  • 2
  • 5

1 Answers1

3

I'm fairly new to ruby myself, however I think this solution meets your needs!

Create a new file in the plugin directory called pages_collection_view_addons.rb (if it doesn't already exist).

Add this to that file:

module PagesCollectionViewAddons
  def chunks(n = 3)
    # Get all the pages
    pages = all
    chunks = []

    # Split the 'pages' array into chunks of size n
    pages.each_slice(n) { |slice|
        chunks.push({pieces: slice})
    }
    chunks
  end
end

# Inform Ruhoh of this new addon
Ruhoh::Resources::Pages::CollectionView.send(:include, PagesCollectionViewAddons)

In your template add something such as:

{{# posts.chunks}}
  <div class="row">
       {{# pieces }}
          <h1>{{ title }}</h1>
       {{/ pieces }} 
  </div>
{{/ posts.chunks }}

This will iterate over each of the chunks where each chunk looks like:

{pieces: [post1, post2, post3]}

Hope this helps.

Hayden
  • 2,818
  • 2
  • 22
  • 30