Instantly share code, notes, and snippets.

Embed
What would you like to do?
Link renderer to be used with will_paginate to render links to work with Twitter Bootstrap
module BootstrapPaginationHelper
class LinkRenderer < WillPaginate::ActionView::LinkRenderer
protected
def page_number(page)
unless page == current_page
link(page, page, :rel => rel_value(page))
else
link(page, "#", :class => 'active')
end
end
def gap
text = @template.will_paginate_translate(:page_gap) { '&hellip;' }
%(<li class="disabled"><a>#{text}</a></li>)
end
def next_page
num = @collection.current_page < @collection.total_pages && @collection.current_page + 1
previous_or_next_page(num, @options[:next_label], 'next')
end
def previous_or_next_page(page, text, classname)
if page
link(text, page, :class => classname)
else
link(text, "#", :class => classname + ' disabled')
end
end
def html_container(html)
tag(:div, tag(:ul, html), container_attributes)
end
private
def link(text, target, attributes = {})
if target.is_a? Fixnum
attributes[:rel] = rel_value(target)
target = url(target)
end
unless target == "#"
attributes[:href] = target
end
classname = attributes[:class]
attributes.delete(:classname)
tag(:li, tag(:a, text, attributes), :class => classname)
end
end
end
@avokhmin

This comment has been minimized.

Show comment
Hide comment
@avokhmin

avokhmin commented Apr 11, 2013

👍

@phildionne

This comment has been minimized.

Show comment
Hide comment
@phildionne

phildionne Jun 14, 2013

Hey, just in case anyone needs this, I implemented the same helper for usage with Sinatra/Padrino.

https://gist.github.com/phildionne/5785087

phildionne commented Jun 14, 2013

Hey, just in case anyone needs this, I implemented the same helper for usage with Sinatra/Padrino.

https://gist.github.com/phildionne/5785087

@coreyhaines

This comment has been minimized.

Show comment
Hide comment
@coreyhaines

coreyhaines Apr 1, 2015

Thanks. Had to change this
tag(:div, tag(:ul, html), container_attributes)
to
tag(:nav, tag(:ul, html, class: "pagination"), container_attributes)
to get it to work with Bootstrap v3.3.34. It worked!

coreyhaines commented Apr 1, 2015

Thanks. Had to change this
tag(:div, tag(:ul, html), container_attributes)
to
tag(:nav, tag(:ul, html, class: "pagination"), container_attributes)
to get it to work with Bootstrap v3.3.34. It worked!

@njt1982

This comment has been minimized.

Show comment
Hide comment
@njt1982

njt1982 commented Nov 11, 2016

@coreyhaines - thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment