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.
Comments
35 responses to “Automated Smarty Pagination”
First thanks to you for sharing you experience with mt. For me this note was very helpful to implement pagination on an mt 4.01 system. But the fact to disable the caching system was unthinkable cause of performance reasons.
Modify the mt core was not allowed for me. So after many times i found a way hacking the mt bootstrap called mtview.php to implement a pagination system based on the code explain in this note and working with the caching system enabled. And it seems to work as well… I paste the code in this comment, i hope it will help someone in the future.
Here is my mtview.php hack :
Hi Yash –
You can do pagination just like you could previously – either with MT-Paginate or with Smarty (as described here). It’s no different from prior versions – at least, I don’t think it is. This site is currently on MT 3.35, so I can’t say for sure.
As to the search results, I’m using Mark Carey’s excellent Fast Search plugin and a few tags to paginate the results. The documentation should describe how to to do it. You can also do the same with MT-Paginate if you don’t have Fast Search.
I dont use MT’s search actually , but I need it since the TAG cloud refers to the search page by searching for the tag. If there is anyway to get a result of entries tagged with that particular tag an paginated (the blog has a large number of entries) then it would do as well.
I am running MT4 and wish to paginate my search results. How would I go about doing so ? Any ideas , I think you run Movable type on this blog, how did you manage to get the ‘navigation’ thingy for the search results? Thanks in advance, this means a lot to me ; I am completely stumped since MT uses CGI for this.
Hi SK –
Actually, it won’t work if you use caching in MT 3.3x either – it’s just that you have to turn that on yourself. ๐
You’ll want to look for this line in mt.php:
Notice that it’s just the $blog_id and $fi_path? There is no page number on it. So you need to add it. Of course, you need to have a page number first.
Once you do, you need to add it to the cache ID, like so:
How do you get a value into $page_number? Well, that’s a little tricky. You can try something simple like this:
This would be the value that you used for the page number in your templates. Just set $page_number equal to that variable and you’re set. Now the cache ID is equal to the blog ID, the fileinfo path and the page number, so now you will have a cached version of each page.
Somehow it does not work with caching enabled on MT4.1. On clicking the next link I get the first page only.
Ok great! It worked. Thanks a lot I really appreciate the help. ๐
If you want to update your Main Index, then that’s where you should put the code. The Page template is for pages.
The error you are getting sounds like you don’t have an </MTEntries> tag. Also, make sure you are using dynamic publishing – it’s not on by default.
Thanks again for the response! I gathered that it would go in my Page template for parsing the main index entries, but just by placing it in there, I get errors because of the MTEntries tag:
“Your template changes have been saved. One or more errors were found in this template. MTEntries with no /MTEntries on line 35.”
Would I perhaps need to wrap your code around the Page Detail module in the Page template with “MTEntries lastn=”3″ offset=”`$smarty.request.offset`”
and then the end tag underneath the module?
Hi Shane –
You’ll put the code wherever you want it to appear. Probably in the Entry template, but it’s ultimately up to you (for instance, if you’re using it in the category or date listing, it would be in the Entry Listing template).