Pagination Using Smarty

As I mentioned yesterday, I’m now using Smarty to provide pagination in my category archives. The code is inserted directly into the template, and (apparently) when it rebuilds the template it parses that code prior to rendering the page. It’s actually quite impressive. Thanks again to Brad, not only for giving me some sample code with which to play, but for showing me it is possible in the first place – I had no idea!

The first thing you need to do is set a Smarty variable with a value, and then you can use that value when creating the MTEntries container, using that variable to build the entries you need:

  {{assign var="offset" value="12"}}
  <MTEntries lastn="12" offset="`$offset`">

Note the double braces around the Smarty assign function. This is roughly akin to the angle brackets around Movable Type template tags or HTML. This takes a variable and assigns a value to it. The next line takes the $offset variable (make sure you include those back-ticks) and uses it while building the MTEntries container.

As you may surmise, this particular example is somewhat useless. The $offset variable won’t change – it will always build the same thing. That’s when you get a little fancier. Using Smarty, you can pick up the value of parameters passed to your page. For instance, consider this URL:

  http://www.cxliv.org/about_a_blog?offset=24

To access the value passsed with the offset parameter, you simply use a special name in Smarty. The best part is that it’s already there – you don’t need to do a thing to get it!

  <MTEntries lastn="12" offset="`$smarty.request.offset`">

Simple as that, you can now create pages of archives, just by including the offset parameter when you access the page. Next, we’ll look at using more of Smarty’s power to automate the process for your users.


Posted

in

Comments

5 responses to “Pagination Using Smarty”

  1. Raj Avatar
    Raj

    please give me a paging query using smarty

  2. Chad Everett Avatar

    That is just a replacement for your standard MTEntries container (where you find this depends on which template you are editing, as well as which version of MT you are using).

    For MT3, it is usually found in a particular template (Main Index, Category Archive, Date-Based Archive). For MT4, it is usually found in the same templates, but they are named somewhat differently (Index, Entry Listing).

    I hope this helps!

  3. smarty noob Avatar
    smarty noob

    Hi there, can you elaborate where to put <MTEntries lastn=”12″ offset=”`$smarty.request.offset`”>, is this inside the loop section, below, php page, tpl page, etc.

    sorry, im noob here

  4. Chad Everett Avatar

    Hi Kensley – I’m afraid that I don’t know what you mean by “html option”. Could you perhaps explain better what you are asking, as it isn’t clear to me. Thanks!

  5. Kensley Avatar
    Kensley

    I’m new to using smarty and was curious if there was a way to call the selection made in “html option” back into a mysql database. I’m looking around on the net, but I havn’t found any good sources that show how to store or call selections back after a user has selected a particular item. Any suggestions would help, thanks.