Six Apart Blog

Dollarshort

Our co-founder and President Mena Trott has been sharing her stories on her personal blog Dollarshort since 2001.

Six Apart News & Events

Building on Movable Type Part 3: Creating Pagination Controls

In last week’s installment of “Building on MovableType," I described how to reuse the display options widget for the storage of your own user preferences. While Movable Type uses this mechanism for display preferences (i.e. the number of items to display on a page, the sort order, etc.), technically it can be used to store any user preference. This week we learn how to reuse the pagination controls widget.

If you are a developer like me you have probably written and re-written the application logic for displaying pagination controls about a million times. When I first wrote Media Manager, I fell prey to re-inventing of the wheel once more and created yet another implementation of “next page" and “previous page" links for displaying items on your queue.

Then one day, while thumbing through the Movable Type source code, I saw how pagination was implementednoting that it was abstracted out into a template file. A couple minutes later I had ripped out my own somewhat fragile pagination control implementation and replaced it with Movable Type’s. Doing so was easy and now my implementation is much more reliable, flexible and portable.

Hacking Pagination Controls

Pagination controls are sufficiently ubiquitous that our own developers saw the value in creating a reusable mechanism for creating the links developers should all be too familiar with: “next page," “previous page," “last page," “first page" and “you are viewing results X to Y out of Z." This reusable framework is not only accessible within Movable Type, but by plugins as well.

To reuse Movable Type’s pagination controls, begin by copying pager.tmpl, found in the tmpl/cms directory into your plugin’s local tmpl directory. Then use the following line of code to insert your pagination controls into the appropriate location in your application template:

<TMPL_INCLUDE NAME="pager.tmpl">

Copying the pager.tmpl template is necessary, as it requires just a small tweak to eliminate some HTML that is typically unnecessary for most plugins.

Once you have included pager.tmpl into your plugin’s template file, all that's left is populating your template context with a small handful of parameters. These parameters are:

Template ParameterDescription
offsetIndicates the number of items to offset the result set by. For example, in a result set containing 20 items with an offset of 7 and a limit of 12 the 7th through 19th items would be returned.
limitIndicates the number of items displayed per page.
next_offsetInstructs pager.tmpl to make the "Next Page" link an active/clickable link.
next_offset_valIndicates the number of items to offset the result set when the user clicks the “Next Page" link.
prev_offsetInstructs pager.tmpl to make the "Previous Page" link an active/clickable link.
prev_offset_valIndicates the number of items to offset the result set when the user clicks the “Previous Page" link.
next_maxIndicates the offset for the first page in the result set. This is almost always 0 (zero).
prev_maxIndicates the offset for the last complete page in the result set. This is almost always <list_end> minus <limit>.
list_startIndicates the index of the first element in the list. This is used when displaying “Showing x of y of z" where list_start is x.
list_endIndicates the index of the last element in the list. This is used when displaying “Showing x of y of z" where list_end is y.
list_totalIndicates the index of the total number of elements in the list. This is used when displaying “Showing x of y of z" where list_total is z.
return_argsA query string fragment that will be appended to the end of every link in the set of pagination control links.

In Summary

Aside from learning how to hack your own pagination controls, there are two key lessons to take away from this installment:

Don’t re-invent the wheel.

This is a cornerstone of good software development. Make your code modular enough so that is easily reused. Nothing says “ugly hack" like code that has been copy-and-pasted around a million times. If you ever you catch yourself hitting “Control-C, Control-V," ask yourself, “Can I write this code so that copy and paste isn't necessary?"

The Movable Type team has tried to adhere to this principal as best it can. Most of the code has been abstracted out into its own template files and subroutines so that it can be reused within Movable Type. One very nice consequence is that Movable Type’s code is easily reused by plugins.

Browse Movable Type’s Source Code

One huge benefit of Movable Type is that the source is available for anyone to browse and inspect. No de-compilers are necessary to learn how Six Apart did something. This makes Movable Type an excellent source for good ideas, tips and tricks, and even example code.

So if you are in need of a feature that is similar to one found in Movable Type, open up a text editor and see how it was done by the Movable Type experts and creators. I know that every time I do I've learned something, and I bet you will too.

7 Comments
Jackie said:
January 8, 2007 10:38 PM

"all that's left is populating your template context with a small handful of parameters"

Can you be more specific? Provide an example perhaps?

January 9, 2007 11:35 AM

No problem at all. To specify template parameters one first instantiates a template object, and then calls the param method for each parameter it needs to add to the template's context. For example:


my $tmpl = $app->init_tmpl('search.tmpl');
$tmpl->param('list_start',$offset + 1);
$tmpl->param('list_end',$offset + (scalar @entry_data));
...


This code can be found in its complete form on code.sixapart.com:

http://code.sixapart.com/svn/mtplugins/trunk/MediaManager/plugins/MediaManager/lib/MediaManager/App.pm

Johny said:
January 15, 2007 4:50 AM

I created my own system like this. This have a lot of advantages.

April 16, 2007 6:04 AM

Thanks for another great article. Greetings I agree with You Johny.

BlogBear said:
June 21, 2007 3:29 PM

If you want to create blog pagination with just 3 plugins without the need for PHP and which you can use for both index and individual archive pages (i.e. comments) check out my article on Blog Pagination with Movable Type at www.creativespirits.com.au/treasurechest/pagination-with-movable-type.html.

I'm using this technique on a lot of blogs I built for customers and it works well.

March 9, 2008 11:47 PM

Thanks for the article! good stuff here

April 3, 2008 6:25 AM

Its good to write such class and use it for all projects with pagination.

Nice article, regards

Leave a Comment