Module: WillPaginate::ViewHelpers
- Defined in:
- lib/will_paginate/view_helpers.rb
Overview
Will Paginate view helpers
Currently there is only one view helper: will_paginate
. It renders the pagination links for the given collection. The helper itself is lightweight and serves only as a wrapper around link renderer instantiation; the renderer then does all the hard work of generating the HTML.
Global options for helpers
Options for pagination helpers are optional and get their default values from the WillPaginate::ViewHelpers.pagination_options hash. You can write to this hash to override default options on the global level:
WillPaginate::ViewHelpers.[:prev_label] = 'Previous page'
By putting this into your environment.rb you can easily translate link texts to previous and next pages, as well as override some other defaults to your liking.
Constant Summary collapse
- @@pagination_options =
default options that can be overridden on the global level
{ :class => 'pagination', :prev_label => '« Previous', :next_label => 'Next »', :inner_window => 4, # links around the current page :outer_window => 1, # links around beginning and end :separator => ' ', # single space is friendly to spiders and non-graphic browsers :param_name => :page, :params => nil, :renderer => 'WillPaginate::LinkRenderer', :page_links => true, :container => true }
Class Method Summary collapse
Instance Method Summary collapse
-
#page_entries_info(collection) ⇒ Object
Renders a helpful message with numbers of displayed vs.
-
#paginated_section(*args, &block) ⇒ Object
Wrapper for rendering pagination links at both top and bottom of a block of content.
-
#will_paginate(collection = nil, options = {}) ⇒ Object
Renders Digg/Flickr-style pagination for a WillPaginate::Collection object.
Class Method Details
.total_pages_for_collection(collection) ⇒ Object
:nodoc:
142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/will_paginate/view_helpers.rb', line 142 def self.total_pages_for_collection(collection) #:nodoc: if collection.respond_to? :page_count and !collection.respond_to? :total_pages WillPaginate::Deprecation.warn <<-MSG You are using a paginated collection of class #{collection.class.name} which conforms to the old API of WillPaginate::Collection by using `page_count`, while the current method name is `total_pages`. Please upgrade yours or 3rd-party code that provides the paginated collection. MSG class << collection def total_pages; page_count; end end end collection.total_pages end |
Instance Method Details
#page_entries_info(collection) ⇒ Object
Renders a helpful message with numbers of displayed vs. total entries. You can use this as a blueprint for your own, similar helpers.
<%= page_entries_info @posts %>
#-> Displaying entries 6 - 10 of 26 in total
134 135 136 137 138 139 140 |
# File 'lib/will_paginate/view_helpers.rb', line 134 def page_entries_info(collection) %{Displaying entries <b>%d - %d</b> of <b>%d</b> in total} % [ collection.offset + 1, collection.offset + collection.length, collection.total_entries ] end |
#paginated_section(*args, &block) ⇒ Object
Wrapper for rendering pagination links at both top and bottom of a block of content.
<% paginated_section @posts do %>
<ol id="posts">
<% for post in @posts %>
<li> ... </li>
<% end %>
</ol>
<% end %>
will result in:
<div class="pagination"> ... </div>
<ol id="posts">
...
</ol>
<div class="pagination"> ... </div>
Arguments are passed to a will_paginate
call, so the same options apply. Don’t use the :id
option; otherwise you’ll finish with two blocks of pagination links sharing the same ID (which is invalid HTML).
123 124 125 126 127 |
# File 'lib/will_paginate/view_helpers.rb', line 123 def paginated_section(*args, &block) pagination = will_paginate(*args).to_s content = pagination + capture(&block) + pagination concat content, block.binding end |
#will_paginate(collection = nil, options = {}) ⇒ Object
Renders Digg/Flickr-style pagination for a WillPaginate::Collection object. Nil is returned if there is only one page in total; no point in rendering the pagination in that case…
Options
-
:class
– CSS class name for the generated DIV (default: “pagination”) -
:prev_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) -
: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 of the link renderer (default: WillPaginate::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) -
:id
– HTML ID for the container (default: nil). Passtrue
to have the ID automatically generated from the class name of objects in collection: for example, paginating ArticleComment models would yield an ID of “article_comments_pagination”.
All options beside listed ones are passed as HTML attributes to the container element for pagination links (the DIV). For example:
<%= will_paginate @posts, :id => 'wp_posts' %>
… will result in:
<div class="pagination" id="wp_posts"> ... </div>
Using the helper without arguments
If the helper is called without passing in the collection object, it will try to read from the instance variable inferred by the controller name. For example, calling will_paginate
while the current controller is PostsController will result in trying to read from the @posts
variable. Example:
<%= will_paginate :id => true %>
… will result in @post
collection getting paginated:
<div class="pagination" id="posts_pagination"> ... </div>
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/will_paginate/view_helpers.rb', line 82 def will_paginate(collection = nil, = {}) , collection = collection, nil if collection.is_a? Hash unless collection or !controller collection_name = "@#{controller.controller_name}" collection = instance_variable_get(collection_name) raise ArgumentError, "The #{collection_name} variable appears to be empty. Did you " + "forget to pass the collection object for will_paginate?" unless collection end # early exit if there is nothing to render return nil unless WillPaginate::ViewHelpers.total_pages_for_collection(collection) > 1 = .symbolize_keys.reverse_merge WillPaginate::ViewHelpers. # create the renderer instance renderer_class = [:renderer].to_s.constantize renderer = renderer_class.new collection, , self # render HTML for pagination renderer.to_html end |