Automated Smarty Pagination

As mentioned previously, using Smarty for pagination isn’t difficult. Of course, the simple example already given doesn’t really do much – your users would still need to know about it, and manually construct the URL. That wouldn’t be much fun. Using Smarty, we can do the work for them!

You’ll need to start with a “total” count. I use MTCategoryCount, as I’m doing this on category archives. If you wanted to do a complete blog archive, you may want to use MTBlogEntryCount or something similar. You could also perform a similar process for comments with MTEntryCommentCount, trackbacks with MTEntryTrackbackCount, etc.

  {{capture assign="count"}}<$MTCategoryCount$>{{/capture}}

This introduces a new function, called capture, that takes the value between the capture tags and assigns that value to a new variable – in this case, called count. Accessing this variable later requires you to put a $ in front of it ($count). So the Movable Type template tag is parsed, a number is created, and that number is assigned to the variable count.

Once you have this total count, it’s easy to create a previous link:

  {{if $smarty.request.offset > 0}}
<a href="?offset={{math equation="max(x-12,0)" x=$smarty.request.offset}}">Previous</a>
{{/if}}

First, this chunk of code checks to see if the offset is greater than 0. If it is not, then it is 0, meaning you’re on the first page – no need for a previous link. If it is, then it uses another Smarty function, math, to create the link. Within the math equation, the PHP max function is used – this function accepts two variables and returns the higher of the two. In this case, it returns the value of offset minus 12 (which indicates a prior page), or if that result is less than zero, it’s the beginning and you get 0.

The next tag is more complex in some ways and less so in others:

  {{if $count > 12 and $smarty.request.offset < $count-12}}
{{if $smarty.request.offset > 0}}
|
{{/if}}
<a href="?offset={{$smarty.request.offset+12}}">Next</a>
{{/if}}

Again we check some numbers. First, to see if the total count is greater than the page count. On the first page of archives, this is necessary – otherwise it would think there was a next page, even if the total count was less than was displayed on the page. This appears to have something to do with the way a variable is handled if it’s never defined (for instance, if you submit a URL without the offset parameter). If you always include offset in the URL, then this piece isn’t needed. The second check is to see if the offset is less than the count (remember, our total number of items) minus 12 (our page value).

If both of these conditions are true, it means we have some more records coming. But first, it checks another condition – namely, to see if the offset is greater than 0. Just like before, this means that we’re on at least the second page of results. That would mean we have a “previous” link, so we need a separator to help readability. Finally, we build the next link, by taking the offset value and adding 12, our value for each page.

Assuming we have multiple pages of data, we’ll see a variety of display options:

  Next
Previous
Previous | Next

With each of those being a link to the appropriate place at the appropriate time.

Just to reiterate, the entire code for this would look something like:

  {{capture assign="count"}}<$MTBlogEntryCount$>{{/capture}}
{{if $smarty.request.offset > 0}}
<a href="?offset={{math equation="max(x-12,0)" x=$smarty.request.offset}}">Previous</a>
{{/if}}
{{if $smarty.request.offset < $count-12}}
{{if $smarty.request.offset > 0}}
|
{{/if}}
<a href="?offset={{$smarty.request.offset+12}}">Next</a>
{{/if}}
<MTEntries lastn="12" offset="`$smarty.request.offset`">

Once you get the basics working, it’s easy to just style the links differently, change the number of items per page or include other information in your template.

The best thing about Smarty is that it goes right into your template files and you can check the results quickly and easily (assuming you are using dynamic publishing, of course). Enjoy.


Posted

in

Comments

35 responses to “Automated Smarty Pagination”

  1. Tito Avatar
    Tito

    Hey! You did a good job I am thankful.

  2. Chad Everett Avatar

    Uh, no (on both counts).

    All you have to do is to give the link (or any html fragment) a class, or id, and then style it. Enjoy.

  3. john Avatar
    john

    Smarty pagination works fine

    Can u please add a new attrubute or funtion such that we can give a css class name for the hyperlinks ,to diferenciate for current page and rest of the page links thru custom colors or etc thru css class name

    when this feature is implemented please email me

    Thanks
    john

  4. skoji Avatar

    Fantastic!

    I tried MTPaginate plugin, but it won’t work with dynamic pages.

    This is THE solution!

    Thanks a lot.

  5. jay Avatar

    I recently migrated my weblog from the university’s servers to my own domain, and I am now paying for the bandwidth that my blog requires.

    Some of my entries have over 500 comments, and therefore wasting an incredible amount of bandwidth on many visitors who don’t even look at the comments.

    So, I was looking for a solution to paginate the individual archives in hopes of optimizing comment delivery—thus serving them only to those who request them—and your wonderful example allowed me to get a test page up and running using dynamic publishing within about 10 minutes!

    I cannot thank you enough, for it will likely save me lots of money. And as I am still a full-time graduate student, that means a lot to me.

    Cheers!

  6. Thomas Avatar

    Wow. I switched to dynamic publishing few days ago, and it is getting interesting…

    The pagination is great! Thank you for the tutorial!

  7. Dhiram Avatar

    Worked like a charm. Thanks a million. 🙂

  8. Chad Everett Avatar

    Interesting – you may notice that I recently implemented a different paging method, but it wasn’t for this reason. Now, instead of using a query string, I simply append the page, and rewrite inside of the PHP. This doesn’t seem to result in any caching issues – but then, the other way didn’t cause me any problems either!

  9. ernie Avatar

    I implemented this and had a problem with caching turned on, where MT would cache all requests as page one, ignoring the query string. I fixed it by changing line 313 of php/mt.php from:

    $cache_id = $blog_id.’;’.$path;

    to:

    $cache_id = $blog_id.’;’.$path.’;’.$_SERVER[‘QUERY_STRING’];

    Once I did this, it worked perfectly with caching. I’d have prefered a fix I could make in my mtview.php template, rather than customizing the MT code, but couldn’t come up with a way.

  10. Ernie Avatar

    Awesome! This is fantastic. Thanks a ton