Smarty and Pagination
One task that always annoyed me has been pagination, it seems to come up in every project and every time I write some other solution for it. Well, I finally figure I’d just write a Smarty extension to take care of it.
This code is called like this:
{paginate count=30 curr=1 max=10 url=http://here.com/page-::PAGE::}
Where the count is the number of total items, curr is the current page, max is the maximum allowed number of displayed page links, and the url is the link to the other pages.
Anyway, below is some code, you can see it in action on KFO’s browse page
<?php
$oTpl->register_function(‘paginate’, ’smarty_paginate’);
function smarty_paginate ($aParam) {
// {paginate count=30 curr=1 max=10 url=http://here.com/page-::PAGE::}
$nPageCnt = $aParam[‘count’];
$nCurrPage = $aParam[‘curr’];
$nMaxPage = $aParam[‘max’];
$sUrl = $aParam[‘url’];
$sOut = ”;
$bDrewDots = false;
if ($nPageCnt > $nMaxPage) {
if (1 > ($nCurrPage - ($nMaxPage /2))) {
$nStart = 1;
$nEnd = $nMaxPage;
} elseif ($nPageCnt < ($nCurrPage + ($nMaxPage /2))) {
$nStart = $nPageCnt - $nMaxPage;
$nEnd = $nPageCnt;
} else {
$nStart = $nCurrPage - ($nMaxPage / 2);
$nEnd = $nCurrPage + ($nMaxPage / 2);
}//if
} else {
$nStart = 1;
$nEnd = $nPageCnt;
}//if
for ($a = $nStart; $a <= $nEnd; $a++) {
if ($a == $nCurrPage)
$sOut .= “<span class=’current’>${a}</span>”;
else
$sOut .= “<a href=’” . str_replace(‘::PAGE::’, $a, $sUrl) . “‘ title=’Go to page ${a}’>${a}</a>”;
}//for
if ($nStart > 3) {
$sOut = “
<a href=’” . str_replace(‘::PAGE::’, 1, $sUrl) . “‘ title=’Go to page 1′>1</a>
<a href=’” . str_replace(‘::PAGE::’, 2, $sUrl) . “‘ title=’Go to page 2′>2</a>
<span>…</span>” . $sOut;
}//if
if ($nEnd < ($nPageCnt - 3)) {
$sOut .= “
<span>…</span>
<a href=’” . str_replace(‘::PAGE::’, $nPageCnt - 1, $sUrl) . “‘ title=’Go to page ” . ($nPageCnt - 1) . “‘>” . ($nPageCnt-1) . “</a>
<a href=’” . str_replace(‘::PAGE::’, $nPageCnt, $sUrl) . “‘ title=’Go to page ” .$nPageCnt . “‘>” .$nPageCnt . “</a>
” ;
// die($sOut);
}//if
if ($nCurrPage == 1)
$sOut = ‘<span class=”nextprev”>« Previous</span>’ . $sOut;
else
$sOut = ‘<a href=”‘ . str_replace(‘::PAGE::’, $nCurrPage - 1, $sUrl) . ‘” class=”nextprev” title=”Go to Previous Page”>« Previous</a>’ . $sOut;
if ($nCurrPage >= $nPageCnt)
$sOut .= ‘<span class=”nextprev”>Next »</span>’;
else
$sOut .= ‘<a href=”‘ . str_replace(‘::PAGE::’, $nCurrPage + 1, $sUrl) . ‘” class=”nextprev” title=”Go to Next Page”>Next »</a>’;
return $sOut;
}//smarty_pagenate
?>


Hi,
This smarty function is great.
I made some changes and put this at plugins folder.
Do not need call register_function anymore…
To do this change the line :
function smarty_paginate ($aParam) {
To this line :
function smarty_function_paginate ($aParam, &$smarty) {
[]´s
Comment by Ronaldo — August 14, 2007 @ 8:34 am
Hi, I based on this script to make one that fits my needs, here it is, hope it’s useful for someone.$i “;
}
}
if (($total % $per_page) != 0)
{
if ($i == $pagina)
{
$sOut .= “$i “;
}
else
{
$sOut .= “$i “;
}
}
return $sOut;
}
?>
Comment by peachepe — December 11, 2007 @ 12:48 pm
Hello!
Can this function be modified in order to use it without mod_rewrite? Something like {paginate count=30 curr=1 max=10 url=http://www.page.com/test.php?page=::PAGE::}
Thanks.
Comment by Cosmin — March 24, 2008 @ 10:32 am