Module: Dimples::Pagination

Included in:
Site
Defined in:
lib/dimples/pagination.rb

Overview

A module that supports pagination.

Defined Under Namespace

Classes: Pager

Instance Method Summary collapse

Instance Method Details

#paginate(site, items, path, layout, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dimples/pagination.rb', line 6

def paginate(site, items, path, layout, options = {})
  context = options.delete(:context) || {}
  url = path.sub(site.output_paths[:site], '') + '/'
  per_page = options.delete(:per_page) ||
             site.config[:pagination][:per_page]

  pager = Pager.new(url, items, per_page, options)

  pager.each do |index, page_items|
    page = Dimples::Page.new(site)

    page.output_directory = if index == 1
                              path
                            else
                              File.join(path, "page#{index}")
                            end

    page.layout = layout
    page.title = options[:title] || site.templates[layout]&.title
    page.extension = options[:extension] if options[:extension]

    context[:items] = page_items
    context[:pagination] = pager.to_h

    page.write(context)
  end
end