← Home

Mephisto Pagination Plugin updated: what's new?

Let me first mention that the plugin now pages tag pages, which I have been asked for by some people. This stuff will just work like expected.

Also, it's worth noting that I am running my blog on Mephisto edge on Rails rev 7405 (i.e. that's Rails edge as of some weeks ago, it's not the Rails 2.0 release candidate!). I have neither tested this with Mephisto 0.7.3 on Rails 1.2.3 nor Rails 2.0 yet. The will_paginate plugin works on Rails 1.2.3 and most probably will work on Rails 2.0, so IMO there's no obvious reason why the Mephisto Paged Article Lists plugin shouldn't do so as well. But I can't garantuee anything like that. Please post your results in the comments if you happen to check this out!

Ok, with this out of the way, let's look at interesting stuff.

Now riding (a liquidized) will_paginate

You most probably know that Rails edge has kicked pagination as a built-in core feature and now requires us to choose from a variety of plugins to get this functionality. This really is a good thing. But of course it means that we have to re-integrate a plugin in our applications.

Rick chose will_paginate as the pagination solution that is shipped with Mephisto right out of the box now. Therefor the most obvious solution was to refactor my plugin to now rely on will_paginate, too.

To get will_paginate working from within Liquid templates I needed to do some wicked trickery though with the WillPaginate::Collection, which I guess probably deserves an article on its own. But in short the Mephisto PagedArticleList plugin now basically does the following:

  • It registers a before_filter which extracts the page parameter from the url path. (This is necessary because of Mephisto's dynamic routes mechanism.)
  • It replaces the MephistoController actions dispatch_list and dispatch_tags with versions that assign paginated collections to the template.
  • It adds a to_liquid method to the WillPaginate::Collection and provides a new WillPaginate::CollectionDrop to be able to access the collection from within the template accordingly.
  • It registers a series of filters to Liquid in order to allow you to use the paginated collection from within your templates.

That's it.

All you need to know though is that you can choose from the following Liquid filters:

Prev/next page links

Like before, there's a convenient prev_next_page_links filter that provides prev/next links like those you can see on this blog. Here's a screenshot:

The usage of this filter has slightly changed because of the WillPaginate collection that we are now using. You can now do:

{{ articles | prev_next_page_links: path_info }}	

You can also provide two alternative link texts and/or a separator to this filter. I.e. the following usages will work as expected:

{{ articles | prev_next_page_links: path_info, 'previous', 'next' }}
{{ articles | prev_next_page_links: path_info, 'previous', 'next', '·' }}
{{ articles | prev_next_page_links: path_info, '·' }}	

Single link filters

Probably you might want to control these links individually. You can then (like before) use the following filters:

{{ articles | link_to_prev_page: path_info }}	
{{ articles | link_to_next_page: path_info }}		

Again, you can inject a different link text. Like so:

{{ articles | link_to_prev_page: path_info, 'previous' }}	
{{ articles | link_to_next_page: path_info, 'next' }}	

Digg-style navigation

As we are riding on the back of will_paginate, only some minor additions were necessary to enable the usage of the will_paginate view helper from within our Liquid templates. That means that now you can do this:

{{  articles | will_paginate: path_info }}

... which will result in a nice list of pagination links. You can now easily add some CSS styles (e.g., like the stylesheet proposed here) and achieve something that looks very similar to the paginators on Digg, Flickr and others.

Probably something like this:

What do you think?

blog comments powered by Disqus