Skip to content

Instantly share code, notes, and snippets.

@chrise86
Forked from corny/will_paginate_bootstrap.rb
Created June 11, 2012 14:29
Show Gist options
  • Save chrise86/2910335 to your computer and use it in GitHub Desktop.
Save chrise86/2910335 to your computer and use it in GitHub Desktop.
WillPaginate renderer for will_paginate 2.3.16 to generate HTML code for Twitter Bootstrap
#
# WillPaginate renderer for will_paginate 2.3.16 to generate HTML code for Twitter Bootstrap
# Put this content in in your config/initializers/will_paginate.rb
module WillPaginate
module ViewHelpers
class BootstrapLinkRenderer < LinkRenderer
def to_html
links = @options[:page_links] ? windowed_links : []
# previous/next buttons
links.unshift page_link_or_span(@collection.previous_page, 'disabled prev_page', @options[:previous_label])
links.push page_link_or_span(@collection.next_page, 'disabled next_page', @options[:next_label])
html = links.join(@options[:separator])
html = html.html_safe if html.respond_to? :html_safe
html = @template.content_tag(:ul, html)
@options[:container] ? @template.content_tag(:div, html, html_attributes) : html
end
def gap_marker
@gap_marker ||= begin
text = @template.will_paginate_translate(:page_gap) { '&hellip;' }
@template.content_tag :li, @template.link_to(text, '#'), :class => 'disabled'
end
end
protected
def page_link_or_span(page, span_class, text = nil)
text ||= page.to_s
text = text.html_safe if text.respond_to? :html_safe
if page and page != current_page
classnames = span_class && span_class.index(' ') && span_class.split(' ', 2).last
page_link page, text, :rel => rel_value(page), :class => classnames
else
page_span page, text, :class => span_class
end
end
def page_link(page, text, attributes = {})
@template.content_tag :li, @template.link_to(text, url_for(page), attributes)
end
def page_span(page, text, attributes = {})
@template.content_tag :li, @template.link_to(text, '#'), :class => 'disabled'
end
end
pagination_options[:renderer] = BootstrapLinkRenderer
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment