A Plugin for Paginating in Smarty

First I’d like to apologize for the lack of updates here in the last weeks, but the days have been very busy. I’ve bought a new car (more details about that as soon as the snow disappears), written a complete publishing platform from scratch in a weekend to help out when Gamer.no got in trouble and in general done a load of stuff. Anyways, this post isn’t about all that, but rather something else I wrote some time ago.

A use case you’ll encounter very often is the act of paginating items, i.e. including a simple “jump to page x, jump to the next page, jump to the previous page” footer. If you’ve ever tried to implement the logic around this in your view, you know that it can get quite extensive. You have several other solutions, such as the PEAR_Pager, which actually looks like a good solution now (with 2.x). Anyways, this is a plugin for Smarty to make generating pagination links easier.

Download the plugin, drop it into your plugins/ folder in your Smarty library directory, and voilá, you have access to the new {paginator} element.

The module is quite configurable, but as I’ve only extended the parts I’ve had use for it our projects, it may still lack a few simple keys.

{paginator hits=$hits offset=$offset total_hits=$articlesFound class_inactive=paginatorInactive class_active=paginatorActive}

The template variables used here are hits, the number of hits shown on this page, offset, the offset from 0 and total_hits, the total number of available hits in the current list. By default the plugin appends ?hits=<hits>&offset=<offset>+<hits> to the current URL, you can give another URL through the url attribute. The class_ attributes provide the CSS classes to use for the elements that enclose the page numbers or links. See the source code (!) for more information about attributes which work.

As I mentioned previously, the plugin is written for my own personal use, so it’s not as streamlined as it could be. Feel free to update it, dissect it, break it, claim it’s yours .. or anything. I’d be happy if you submit any patches to me so I can update the link here, or simply leave a comment.

Hack aways! You can see the plugin in action at the bottom of lovethatfun.com.

5 thoughts on “A Plugin for Paginating in Smarty”

  1. The only thing included of code in the above text is just that, a sample of usage :-)

    To use the plugin, just drop it in your plugins/-folder in Smarty, then add a tag to use the paginator:

    {paginator hits=<number of hits> offset=<starting offset> total_hits=<total hits>}

    See the example above or the source code for other properties. If you write a nice tutorial I’ll link it here.

  2. There is no select from part – the plugin does not operate on any data. You provide it with the key information about the query you’re running, such as the number of hits (shown on the current page), the offset (where in the query result you are) and the total number of hits (how many documents / items you’re showing in total).

    This is all you need to create the pagination, so there is no need to involve any SQL or Model operations in the plugin itself. It’s your responsibility to get this information in the way that’s suitable for you (from a database, from an API, etc).

Leave a Reply

Your email address will not be published. Required fields are marked *