Method: WillPaginate::ViewHelpers#will_paginate

Defined in:
lib/will_paginate/view_helpers.rb

#will_paginate(collection, options = {}) ⇒ Object

Returns HTML representing page links for a WillPaginate::Collection-like object. In case there is no more than one page in total, nil is returned.

Options

  • :class – CSS class name for the generated DIV (default: “pagination”)

  • :previous_label – default: “« Previous”

  • :next_label – default: “Next »”

  • :inner_window – how many links are shown around the current page (default: 4)

  • :outer_window – how many links are around the first and the last page (default: 1)

  • :link_separator – string separator for page HTML elements (default: single space)

  • :param_name – parameter name for page number in URLs (default: :page)

  • :params – additional parameters when generating pagination links (eg. :controller => "foo", :action => nil)

  • :renderer – class name, class or instance of a link renderer (default in Rails: WillPaginate::ActionView::LinkRenderer)

  • :page_links – when false, only previous/next links are rendered (default: true)

  • :container – toggles rendering of the DIV container for pagination links, set to false only when you are rendering your own pagination markup (default: true)

All options not recognized by will_paginate will become HTML attributes on the container element for pagination links (the DIV). For example:

<%= will_paginate @posts, :style => 'color:blue' %>

will result in:

<div class="pagination" style="color:blue"> ... </div>

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/will_paginate/view_helpers.rb', line 71

def will_paginate(collection, options = {})
  # early exit if there is nothing to render
  return nil unless collection.total_pages > 1

  options = WillPaginate::ViewHelpers.pagination_options.merge(options)

  options[:previous_label] ||= will_paginate_translate(:previous_label) { '&#8592; Previous' }
  options[:next_label]     ||= will_paginate_translate(:next_label) { 'Next &#8594;' }

  # get the renderer instance
  renderer = case options[:renderer]
  when nil
    raise ArgumentError, ":renderer not specified"
  when String
    klass = if options[:renderer].respond_to? :constantize then options[:renderer].constantize
      else Object.const_get(options[:renderer]) # poor man's constantize
      end
    klass.new
  when Class then options[:renderer].new
  else options[:renderer]
  end
  # render HTML for pagination
  renderer.prepare collection, options, self
  output = renderer.to_html
  output = output.html_safe if output.respond_to?(:html_safe)
  output
end