日期:2014-03-03  浏览次数:20543 次

一、扩展你的Smarty
1、准备功夫

PHP代码:--------------------------------------------------------------------------------
function Smarty_function_page ( $params, &$Smarty )
{
$href = '#';
$space = ' ';

$frist = NULL;
$last = NULL;

$page = 5;

extract($params);

if ( !$row || $row <= 1 ) return ' ';

$pages = $row;
$curr_page = $now ? $now : 1;
$offset = 2;
$from = $curr_page - $offset;
$to = $curr_page + $page - $offset - 1;

if ( $page > $pages )
{
$from = 1;
$to = $pages;
}
else
{
if ( $from < 1)
{
$to = $curr_page + 1 - $from;
$from = 1;
if ( ( $to - $from ) < $page && ( $to - $from ) < $pages )
{
$to = $page;
}
}
elseif ( $to > $pages )
{
$from = $curr_page - $pages + $to;
$to = $pages;
if ( ( $to - $from ) < $page && ( $to - $from) < $pages )
{
$from = $pages - $page + 1;
}
}
}

if ( $frist && ( $curr_page - 1 ) >= 1 ) $p['frist'] = '<a href="' . $href . '1">' . $frist . '</a>';
if ( $prev && ( $i = $curr_page - 1 ) >= 1 ) $p['prev'] = '<a href="' . $href . $i . '">' . $prev . '</a>';
for( $i = $from; $i <= $to; $i ++ )
{
if ( $i == $curr_page )
{
$p[$i] = '<a href="' . $href . $i . '" class="nowpage" target="_self">[' . $i . ']</a>';
}
else
{
$p[$i] = '<a href="' . $href . $i . '" target="_self">' . $i . '</a>';
}
}
if ( $next && ( $i = $curr_page + 1 ) <= $pages ) $p['next'] = '<a href="' . $href . $i . '" target="_self">' . $next . '</a>';
if ( $last && ( $curr_page + 1 ) <= $pages ) $p['last'] = '<a href="' . $href . $pages . '" target="_self">' . $last . '</a>';

return implode( $space, $p );
} // end func

--------------------------------------------------------------------------------


将上面的代码命名为"function.page.php"保存到Smarty的plugins目录里


代码:--------------------------------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
{page row=10}
{page row=10 now=5}
{page row=10 now=5 href="plugins.php?a=1&b=2&page=" frist="第一页" prev="上一页" next="下一页" last="最后页"}
{page row=10 now=5 href="plugins.php?a=1&b=2&page=" frist="第一页" prev="上一页" next="下一页" last="最后页"}
{page row=10 now=1 href="plugins.php?a=1&b=2&page=" frist="第一页" prev="上一页" next="下一页" last="最后页"}
{page row=10 now=10 href="plugins.php?a=1&b=2&page=" frist="第一页" prev="上一页" next="下一页" last="最后页"}
</BODY>
</HTML>
--------------------------------------------------------------------------------


将上面的代码命名为"plugins.html"