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. Shane Avatar

    Thanks a lot, Chad. I’m now using it in 3.2 templates but upgraded to 4.01. I’ve created a new 4.0 template blog…Where in the new template system would I put the code?

    Thanks!

  2. Chad Everett Avatar

    Hi Shane –

    This isn’t a plugin. The Smarty templating language is what is used by Movable Type to render dynamic pages. So it’s built in to every distribution of MT. Just start writing by adding the code above to your templates, then save and refresh!

  3. Shane Avatar
    Shane

    How do I install this plugin and where do I find it?

  4. Chad Everett Avatar

    That’s an interesting method, and I commend you for the style you’re using. If nothing else, it may improve the user experience.

    But I’d suggest that you’re not doing a entirely server-based process, either, since it is based on JavaScript.

    By using Smarty (and, in turn, PHP), it’s entirely server-based, and also your server only serves up the part of the page that is requested (like MT-Paginate, which you mention), resulting in lower bandwidth usage.

    Your method returns the entire page – though admittedly, it won’t need to request anything else if the user decides to view another page.

    But it’s quite possible to use it on comments or anything else as well.

    And no hacking is necessary – I simply did so in order to create different URLs. In other words, I didn’t want to include “page=2” or something, so I needed to do some hacking to the code to make it work right. You could also use some mod_rewrite magic that should make this work correctly, depending on the host. Still, thanks for the information!

  5. BlogBear Avatar

    Pagination is one of the most sought after – yet missing – features of MT.

    I’ve created a solution with just three plugins which you can use on index pages (also monthly index, category index) and individual entry pages (e.g. to paginate comments).

    You don’t have to hack MT, don’t need to use dynamic (PHP) publishing either.

    Blog Pagination with Movable Type

  6. Pai Avatar
    Pai

    Thanks Chad…

    Anyway, I wonder why Six Apart didn’t provide this feature… pagination “should” exist by default in any publishing platform.

  7. Chad Everett Avatar

    If you are using static publishing, then that is your problem. Smarty will only work with dynamic publishing. The extension doesn’t matter.

    If you want to continue using static publishing, you may want to try MT-Paginate instead, as you can use it within static archives (you do need to have your pages parsed as PHP).

  8. Pai Avatar
    Pai

    It says that Smarty has been built-in in mt3.33… But why none of my Smarty code being rendered? It just display pure code, not the output…

    I think it’s about static/dynamic publishing, tried both still the same, tried html/php extension, still no different…

    All I want to do is paginate my category page (static publishing), view 10 entries per page, then next/previous…

  9. Yash Avatar

    Thanx tons! Would not have been possible without you.

  10. greg.org Avatar

    This is great, I’ve been getting slammed by my new hosting company because of MTPaginate’s insane resource hogging, and so I’ve been looking for a painless pagination alternative.

    So will you please update my code and email me when you’re done? Thanks. 😉