Formatting/styling will_paginate

16 04 2008


I received few requests in the past ( personally and recently a comment on my post describing how to use will_paginate plugin ) about how to format pagination links while using will_paginate plugin.

So here it is:

When you add

<%= will_paginate @obj_instance %>

in your views to show your pagination links then it also creates an element (div) and wraps the whole thing under the class name pagination. The one with the current page will have the class name current.

Here is the html code generated by will_paginate


<div class=”pagination”>
<a href=”/page_path?page=2″>« Previous</a>
<a href=”/page_path”>1</a>
<a href=”/page_path?page=2″>2</a>
<span class=”current”>3</span>
<a href=”/page_path?page=4″>4</a>
<a href=”/page_path?page=5″>5</a>
<a href=”/page_path?page=4″>Next »</a>
</div>

Now, it is totally upto your imagination how you want to style it using CSS.

But, if due to some reason you don’t want to use the class pagination and add your own then do something like this


<%= will_paginate @obj_instance, :class => ‘my_class’ %>

Similarly, if you want to add an element id then

<%= will_paginate @obj_instance, :id => ‘my_element’ %>

Let’s say if you have done both like so:

<%= will_paginate @obj_instance, :id => ‘my_element’, :class => ‘my_class’ %>

then the new html generated by will_paginate will be

<div class=”my_class” id=”my_element”>
<a href=”/page_path?page=2″>« Previous</a>
<a href=”/page_path”>1</a>
<a href=”/page_path?page=2″>2</a>
<span class=”current”>3</span>
<a href=”/page_path?page=4″>4</a>
<a href=”/page_path?page=5″>5</a>
<a href=”/page_path?page=4″>Next »</a>
</div>

Yes, it is that simple.


Actions

Information

6 responses to “Formatting/styling will_paginate”

16 04 2008
Pagination in Ruby On Rails using will_paginate plugin « class Nasir < Jamal (09:27:07) :

[...] Formatting / Styling pagination links Sign up and be a part of SPhred.com and don’t forget to invite your friends [...]

28 04 2008
Sviergn Jiernsen (21:45:04) :

(Grrrr… escaping this time so the post is readable.)

So, in other words, you are stuck with the “<<Previous” and “Next>>” link texts for the previous and next page links. How can it be said that formatting/styling is enabled if you can’t even override those? What if I want just the little “<<” and “>>” for the previous and next page links? What if I want images for the next and previous links? What if I want text in another language? Any ideas? I could wrap the will_paginate tag manually in a DIV tag of my own creation and get the same effect as this. This doesn’t buy anything, but control over link text would.

28 04 2008
nasir (22:10:15) :

No, you are not stuck with the << Next or Previous >> links. You can override these defaults like so :


<%= will_paginate @obj_instance, :prev_label => '<<', :next_label => '>>' %>

That’s it, just give what ever value you like to prev_label and next_label symbols.

Similarly, if you want to add images then do this

<%= will_paginate @obj_instance, :prev_label => image_tag('n9.gif'), :next_label => image_tag('n8.gif') %>

As far as styling is concerned you can do a lot with CSS.

Hope that was helpful.

24 05 2008
kienes (13:22:08) :

YES THAT S WHAT I HAVE BEEN SEARCHING SINCE LUNCH

24 05 2008
kienes (13:24:37) :

but one question, where can i find alle the possible option for will_paginate for example to exclude the numbers for getting just the prev and next.

24 05 2008

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>