Saturday, August 15, 2009

Pagination with Smarty

I've spent the last hour or two on this. It's a Smarty template that takes an associative array with these keys:
  • curPage: The number of the current page
  • url: The url to which it will make the links. It replaces "%x" with the page number in the URL, so this can be something like /news/page-%x/ or /news.php?page=%x
  • totalPages: The number of total pages.
It also has a few variables that tweak its output (these are set in the template itself, using {assign}):
  • putDots: If the distance between the current page and the first/last page is greater than this, it will put dots towards the end
  • border: when it puts dots around the current page, this is the number of pages that appear around it until the dots start.
I'm too tired and bored to explain anything else, but if anyone has any questions, I'll be glad to answer them. Here's the template: {assign var="putDots" value=3} {assign var="border" value=2} {assign var="curPage" value=$pagination.curPage} {assign var="url" value=$pagination.url} {assign var="totalPages" value=$pagination.totalPages} {if $totalPages > 1} <div class="pages"> <span> {if $curPage > 1} <a title="Previous Page" href="{$url|replace:'%x':$curPage-1}">&laquo;&laquo;</a> {/if} {* Handle the first part of the pages -- up to the current one *} {if $curPage > $putDots} <a title="Page 1" href="{$url|replace:'%x':'1'}">1</a> ... {section name=i start=$curPage-$border loop=$curPage} {assign var="curPos" value=$smarty.section.i.index} <a title="Page {$curPos}" href="{$url|replace:'%x':$curPos}">{$curPos}</a> {/section} {else} {section name=i start=1 loop=$curPage} {assign var="curPos" value=$smarty.section.i.index} <a title="Page {$curPos}" href="{$url|replace:'%x':$curPos}">{$curPos}</a> {/section} {/if} {* Current page *} <a title="Page {$curPage}" class="current" href="{$url|replace:'%x':$curPage}">{$curPage}</a> {* Handle the last part of the pages -- from the current one to the end *} {if $totalPages - $curPage + 1 > $putDots} {section name=i start=$curPage+1 loop=$curPage+$border+1} {assign var="curPos" value=$smarty.section.i.index} <a title="Page {$curPos}" href="{$url|replace:'%x':$curPos}">{$curPos}</a> {/section} ... <a title="Page {$totalPages}" href="{$url|replace:'%x':$totalPages}">{$totalPages}</a> {else} {section name=i start=$curPage+1 loop=$totalPages+1} {assign var="curPos" value=$smarty.section.i.index} <a title="Page {$curPos}" href="{$url|replace:'%x':$curPos}">{$curPos}</a> {/section} {/if} {if $curPage < $totalPages} <a title="Next Page" href="{$url|replace:'%x':$curPage+1}">&raquo;&raquo;</a> {/if} </span> </div> {/if}

5 comments:

  1. how to use this in my files, as this have no PHP code for calculating the pages number etc

    ReplyDelete
  2. Calculating the pages number is up to you. This is just a template that takes said variables and creates pagination links.

    ReplyDelete
  3. I'm using a new version of Smarty and I needed to replace {$url|replace:'%x':$curPage-1} with {$url|replace:'%x':($curPage-1)} and {$url|replace:'%x':$curPage+1} with {$url|replace:'%x':($curPage+1)}

    Thanks!
    - Bret

    ReplyDelete
  4. what is the value of $pagination

    ReplyDelete
  5. You not attahched PHP code, this is make us confused, wheres value of $pagination?

    ReplyDelete