<?php require '../libs/Smarty.class.php'; $smarty = new Smarty; $smarty->force_compile = true; $smarty->debugging = true; $smarty->caching = true; $smarty->cache_lifetime = 120; $count = 100; $pagesize = 10; $totalpage = 10; $url = 'index.php'; $left = 3; $right = 3; if (isset($_GET['page'])) { $page = $_GET['page']; } else { $page = 1; } //赋值 $smarty->assign('count', $count); //记录总数 $smarty->assign('pagesize', $pagesize); //每页记录数 $smarty->assign('totalpage', $totalpage); //总页数 $smarty->assign('page', $page); //当前页数 $smarty->assign('url', $url); //链接地址 $smarty->assign('left', $left); //数字链接左侧长度 $smarty->assign('right', $right); //数字链接右侧长度 //显示 $smarty->display('main.tpl'); ?>
模板文件1:main.tpl
{*分页内容展示*} {*传递变量*} {include 'page.tpl' count=$count pagesize=$pagesize totalpage=$totalpage page=$page url=$url left=$left right=$right}
模板文件2: page.tpl
{if $totalpage>1} <a href="{$url}">首页</a> {if $page>1} <a href="{$url}?page={$page-1}">上一页</a> {/if} {*数字链接*} {if $totalpage<$left+$right+1} {*总页数小于数字链接长度时*} //定义循环起始值 {assign var="start" value=1} {assign var="end" value=$totalpage} {else} {assign var="start" value=$page-$left} {assign var="end" value=$page+$right} {if $page <= $left} {*当前页左侧长度不足指定值时 *} {assign var="start" value=1} {assign var="end" value=$left+$right+1} {/if} {if $page >= $totalpage- $right} {*当前页右侧长度不足指定值时*} {assign var="end" value=$totalpage} {assign var="start" value=$totalpage-$left-$right} {/if} {/if} {*循环输出数字链接 *} {for $foo=$start to $end} {if $page == $foo} <a href="{$url}?page={$foo}">{$foo}</a> {else} <a href="{$url}?page={$foo}">{$foo}</a> {/if} {/for} {if $page<$totalpage} <a href="{$url}?page={$page+1}">下一页</a> {/if} <a href="{$url}?page={$totalpage}">尾页</a> {/if}